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]=1; in[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;
}

HDU 4948 (傻比图论),布布扣,bubuko.com

时间: 2024-08-04 05:29:22

HDU 4948 (傻比图论)的相关文章

2014多校联合八(HDU 4945 HDU 4946 HDU 4948 HDU 4950 HDU 4951 HDU 4952)

HDU 4945 2048 题意:给你一堆数字  问有几个子集可以拼出2048 思路: 拼数字的规则相当于让数字乘二  所以不是2^i的数字不会拼出2048  那么这些数可选可不选  即为2^cnt种可能 之后只要计算出有几个子集不可能拼出2048即可  不过简单的直接dp是2048*100000的复杂度的  会TLE 所以要变成先枚举元素  再枚举该种元素个数  再枚举2048种状态  然后利用组合求(组合里需要逆元) 为什么这样快?  因为比如枚举2^5这个元素的时候  最多取2^6个  枚

HDU 2647 Reward(图论-拓扑排序)

Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards. The workers will compare their rewards ,a

HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10    Accepted Submission(s): 3 Problem Description There are N bombs needing exploding. Each bomb has three attributes: exploding radius ri,

HDU 5961 传递 【图论+拓扑】 (2016年中国大学生程序设计竞赛(合肥))

传递 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description 我们称一个有向图G是传递的,当且仅当对任意三个不同的顶点a,,若G中有 一条边从a到b且有一条边从b到c ,则G中同样有一条边从a到c.我们称图G是一个竞赛图,当且仅当它是一个有向图且它的基图是完全图.换句 话说,将完全图每条边定向将得到一个竞赛图.下图展示的是一个有4个顶点的竞

【复习】深夜复习傻逼图论的玩意

不写点东西明天要挂啊 不想看傻逼的直接跳到后面   0.存储结构(你妈写这个凑字数呀 邻接表&插入边 struct edge{ int to,next,v; }e[M]; int cnt,last[N]; void insert(int a,int b,int c){ e[++cnt]=(edge){b,last[a],c};last[a]=cnt; } void link(int a,int b,int c){ insert(a,b,c);insert(b,a,c); } 1.闭着眼睛摸了个s

hdu 4948

Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 500    Accepted Submission(s): 231Special Judge Problem Description Teacher Mai has a kingdom consisting of n cities. He has planned the t

HDU 2767 Proving Equivalences 图论scc缩点

问一个图,最少需要加多少条边,使得这个图强联通. Tarjan缩点,重建图,令a=入度为0的scc个数,b=出度为0的scc个数,ans=max(a,b): 若图scc=1,本身强联通,ans=0: 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAXN = 20010;//点数 4 const int MAXM = 200100;//边数 5 struct Edge { 6 int to,next; 7 }edge[

多校第八场:图论出度

HDU 4948 这题比赛的时候遗憾了,我看了这道题,然后觉得挺简单的. 刚开始一看题上,想到的就是拓扑排序,然后脑子想啊想--感觉就是拓扑排序的逆序,然后发现挺水的-- 因为说了要想发展某个城市的话,就必须有另一个城市作为它发展的前提,即城市u->w这样连边,表示要想发展城市w,前提是u已经是发展过的城市了.那这样的话不是很简单嘛. 即统计出出度最多的就是第一个要发展的城市了,因为u->w这样连边可以看出算出出度最多的依次从大到小排序就行了. 哎呀,不过可惜了,因为看见没人交这题,然后也不敢

多第八田间学校:几何+图论出度+模拟+找到规律

HDU 4946 pid=1002&cid=509" style="color:rgb(26,92,200); text-decoration:none; font-family:Tahoma; background-color:rgb(215,235,255)">Area of Mushroom 这题WA了7发才过,队友做的,然后一起debug了好久. 刚開始是没排序. 然后是在同一个位置的点没有处理好. 然后把这两个问题搞定就A了. #include <