hdu4948 Kingdom

题意:有n个城市,任意两点之间有且仅有一条有向边。要求输出一种建造城市的顺序,使得之前已经建造的城市可以到达当前建造的城市,且至多经过两条边。

首先我们可以证明,这种方案是肯定存在的,

因为在一个满足题意的图中,入度最大的点一定是其他点在两步之内可达的。那么这个点就最后输出。

上面的结论是为什么呢。。题解告诉我们用反证法证明,

若u结点是当前图中入度最大的结点,假设v点存在该路径v->a->b->u,

由于u是入度最大的结点,那么v 不可能连到u以及 b或者与u直接相连的点(不然v就可以两步到达u了),

由于两点间有一条有向边,v连布到他们,那么他们到v一定有边,既v的入度至少是 u+指向u的点,既v的入度大于u的,这里矛盾了。

所以我们可以知道,在任何一个符合题意的图中,都可以知道最后输出的点,

我们每找到一个点,就标记,把与该点相连的边删去。这样找完了倒序输出就i可以了。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:16777216")
#define eps 1e-6
#define ll long long
using namespace std;

const int maxn=505;

int n,in[maxn],vis[maxn];
char s[maxn][maxn];

int main()
{
    int i,j;
    while(~scanf("%d",&n)&&n)
    {
        memset(in,0,sizeof in);
        for(i=1;i<=n;i++)
        {
            scanf("%s",s[i]+1);
            for(j=1;j<=n;j++)
                if(s[i][j]=='1')
                    in[j]++;
        }
        vector<int> ans;
        memset(vis,0,sizeof vis);
        int maxin,p,flag=1;
        while(flag)
        {
            flag=0,maxin=-1,p=0;
            for(i=1;i<=n;i++)
            {
                if(!vis[i]&&maxin<in[i])
                {
                    flag=1;
                    maxin=in[i];
                    p=i;
                }
            }
            if(flag)
            {
                vis[p]=1;
                ans.push_back(p);
                for(i=1;i<=n;i++)
                {
                    if(i==p) continue;
                    if(s[p][i]=='1') in[i]--;
                }
            }
        }
        printf("%d",ans[ans.size()-1]);
        for(i=ans.size()-2;i>=0;i--)
            printf(" %d",ans[i]);
        puts("");
    }
    return 0;
}

/*
4
0111
0011
0001
0000

4
0100
0010
1001
1100

1 2 3 4
3 4 1 2
*/

hdu4948 Kingdom

时间: 2024-10-12 01:16:50

hdu4948 Kingdom的相关文章

Constructing Roads In JGShining&#39;s Kingdom HDU - 1025

JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities)

hdu-1025 Constructing Roads In JGShining&#39;s Kingdom(二分查找)

题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21045    Accepted Submission(s): 5950 Problem Description JGShining's kingdom consists of 2n(n is

类Kingdom Rush塔防游戏的数值设计(一)&mdash;&mdash;玩法概述和基础战斗模型建立

玩法概述---- Kingdom Rush,即皇家守卫军,IronHide出的一款塔防游戏.其涂鸦的风格.独特的英雄和士兵系统.较为庞大的战斗场面,在塔防游戏中独树一帜,取得了巨大的成功. 在这里,基于Kingdom Rush的核心玩法,推拟出游戏所需的核心战斗数值(包括战斗内成长数值). 首先,需要明确游戏的玩法. 游戏大概的战斗场景如下 战斗地图区域划分 1.出怪点:2.预设路径:3.防御塔建造点:4.防守点 战斗单位划分 我方 1.防御塔:2.英雄:3.士兵 敌方 1.怪物 游戏玩法概述

13杭州区域赛现场赛Rabbit Kingdom(树状数组+离线)

题意:给你一个长度数列,再给你m个询问(一个区间),问你在这个区间里面有多少个数与其他的数都互质. 解题思路:你看这种类型的题目都可以肯定这是 离线+树状数组(线段树).主要就是他的更新信息.这里我的处理是先把1-200000(每个数的范围)数里面所有的质因子求出来.然后从后往前遍历数组.会出现以下几种情况 1.a[k]的质因子在后面出现过而[更新标记] 和[被更新标记] 都为假 2.a[k]的质因子在后面出现过的那个位置 I   [更新标记]为 真 . 3.a[k]的质因子在后面出现过且那个位

hdu 5943 Kingdom of Obsession 二分图匹配+素数定理

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description There is a kindom of obsession, so people in this kingdom do things very strictly. They name themselves in integer, and there

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very

codeforces 613D:Kingdom and its Cities

Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daug

杭电1025Constructing Roads In JGShining&#39;s Kingdom

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1025 题目: Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich

Constructing Roads In JGShining&#39;s Kingdom(HDU 1025 LIS nlogn方法)

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21002    Accepted Submission(s): 5935 Problem Description JGShining's kingdom consists of 2n(n is no mor