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.
Fabulous DAGy |
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 ( T40 ), the number of tests. You are given N and M ( 1N400 ) 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 ( 0u , 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;
}