uva 11893 - Fabulous DAGy

  Fabulous DAGy 

Little poopi had something strange that was called  DAGy . poopi liked DAGy so much, but when he showed DAGy to other children they scared and ran away! DAGy is not a pet or a toy; it is a special kind of graph! DAGy is made up of a directed acyclic graph plus one additional directed edge. With this additional edge a cycle forms that goes through every vertex in the graph.

Once when poopi was playing with DAGy, it fell out of his hands and became totally deformed. He cried and cried. He denied new graphs because he wanted his own DAGy.

It is said that computer programmers are supermen, because they can solve problems that nobody else is able to even approach. You, the computer programmer! Help little poopi and dispose his DAGy again!

Input 

In the first line there is an integer  T  ( T$ \le$40 ), the number of tests. You are given  N  and  M  (   1$ \le$N$ \le$400 ) in the first line of each test, which are the number of vertices and the number of edges respectively. Next  M pairs of integers  u v  ( 0$ \le$u v < N ) meaning that there is an edge from vertex  u  to vertex  v . There is at most one edge between each pair of vertices. It is guaranteed that each input graph is a directed acyclic graph with one additional edge between two distinct vertices of graph.

Output 

DAGy can be put back in order if you find the maximal cycle that goes through every vertex. If you found such a cycle print " Yeah, I'm superman " in a single line. Otherwise print " Your DAGy was initially defected! " (Quotes for clarity) You are superman trying to help little poopi anyway!

Sample Input 

2
3 3
0 1
1 2
2 0
4 5
0 1
1 2
2 0
0 3
3 2

Sample Output 

Yeah, I'm superman
Your DAGy was initially defected!

巨坑的解题报告。。

可以转化为,判断一个有向图是否能构成一个长度为n的路径,用拓扑排序。

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 400 + 5;
const int INF = 1000000000;

int n, m;
int M[maxn][maxn];
int ou[maxn], in[maxn];


bool toposort(int source){
    queue<int> q;
    q.push(source);
    int cnt = 0;
    int last = -1;
    while(!q.empty()){
        int pos = q.front();
        q.pop();
        last = pos;
        cnt++;
        for(int i = 0;i < n;i++){
            if(i == source) continue;
            if(M[pos][i] == 1){
                in[i]--;
                if(in[i] == 0) q.push(i);
            }
        }
        if(q.size() > 1) return false;
    }
    if(cnt != n || M[last][source] == 0) return false;
    return true;
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        memset(M, 0, sizeof(M));
        while(m--){
            int x, y;
            scanf("%d%d", &x, &y);
            M[x][y] = 1;
        }
        int ans = 0;
        memset(ou, 0, sizeof(ou));
        memset(in, 0, sizeof(in));
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                if(M[i][j] == 1){
                    in[j]++;
                    ou[i]++;
                }
            }
        }
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                if(in[i] == 1 && ou[j] == 1 && M[j][i] == 1){
                    ans = toposort(i);
                    break;
                }
            }
        }
        if(ans == 1){
            printf("Yeah, I'm superman\n");
        }
        else{
            printf("Your DAGy was initially defected!\n");
        }
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值