首页 > 代码库 > HDU 4948 (傻比图论)

HDU 4948 (傻比图论)

Kingdom


Problem Description
Teacher Mai has a kingdom consisting of n cities. He has planned the transportation of the kingdom. Every pair of cities has exactly a one-way road.

He wants develop this kingdom from one city to one city.

Teacher Mai now is considering developing the city w. And he hopes that for every city u he has developed, there is a one-way road from u to w, or there are two one-way roads from u to v, and from v to w, where city v has been developed before.

He gives you the map of the kingdom. Hope you can give a proper order to develop this kingdom.
 

Input
There are multiple test cases, terminated by a line "0".

For each test case, the first line contains an integer n (1<=n<=500).

The following are n lines, the i-th line contains a string consisting of n characters. If the j-th characters is 1, there is a one-way road from city i to city j.

Cities are labelled from 1.
 

Output
If there is no solution just output "-1". Otherwise output n integers representing the order to develop this kingdom.
 

Sample Input
3 011 001 000 0
 

Sample Output
1 2 3

 

 题意 :给出一个图满足两两之间都有一条边,然后选择建设的城市,满足当前选的城市和之前选的城市之间最多距离为2.  求建设城市的顺序。

 sl :刚开始傻逼了没看到红色的话,比赛的时候更傻逼,提都理解错了。妈蛋白敲了100+代码。 其实选下最大度数的节点就好了,证明就是题解

的那个证明,反证。因为确保都有一条边所以题解是对的,我说开始为什么看着不对呢 

 

 #include <cstdio>

#include <cstring>
#include <algorithm>
#include <vector>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int MAX = 500+10;
char str[MAX][MAX];
int in[MAX],vis[MAX],G[MAX][MAX];
vector<int> res;
int main() {
    int n;
    while(scanf("%d",&n)==1&&n) {
        memset(G,0,sizeof(G));
        memset(vis,0,sizeof(vis));
        memset(in,0,sizeof(in));
        res.clear();
        for(int i=1;i<=n;i++) {
            scanf("%s",str[i]+1);
        }
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                if(str[i][j]==1) {
                     G[i][j]=1in[j]++;
                }
            }
        }
        for(int i=1;i<=n;i++) {
            int node,Max=0;
            for(int j=1;j<=n;j++) {
                if(!vis[j]) {
                    if(Max<=in[j]) {
                        node=j; Max=in[j];
                    }
                }
            }
            for(int j=1;j<=n;j++) if(G[node][j]) in[j]--;
            vis[node]=1;
            res.push_back(node);
        }
       // printf("s");
        for(int i=res.size()-1;i>=0;i--) {
            if(!i) printf("%d\n",res[i]);
            else printf("%d ",res[i]);
        }
    }
    return 0;
}