#include<cstdio>
#include<vector>
#include<memory.h>
using namespace std;
#define MAX_NODE 100001
struct Edge{
int to, can;
Edge(int to) {this->to = to, can = 1;}
};
int dfn[MAX_NODE], nextdfn;
vector<Edge> g[MAX_NODE];
bool visited[MAX_NODE];
int dfs(int u, int p) {
int i, minback;
dfn[u] = nextdfn++;
minback = nextdfn;
for(int i = 0 ; i < g[u].size() ;i++) {
int v = g[u][i].to;
if( v == p ) continue;
if( dfn[v] == -1 ) {
int back = dfs(v,u);
if( back > dfn[u] ) {
g[u][i].can = 0;
for(int j = 0 ; j < g[v].size() ; j++) {
if( g[v][j].to == u ) g[v][j].can = 0;
}
}
minback = min(minback, back);
}
else minback= min(minback, dfn[v]);
}
return minback;
}
int verNum = 0, chk = 1;
void dfs2(int u) {
if(visited[u]) return;
visited[u] = 1;
verNum++;
int num = 0;
for(int i = 0 ; i < g[u].size() ; i++) {
if(g[u][i].can == 0) continue;
int v = g[u][i].to;
dfs2(v);
num++;
}
if(num != 2) chk = 0;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
memset(dfn, -1, sizeof(dfn));
for(int i = 0 ; i < m ; i++) {
int st, end;
scanf("%d%d", &st, &end);
g[st].push_back(Edge(end));
g[end].push_back(Edge(st));
}
dfs(1,-1);
for(int i = 1 ; i <= n ; i++) {
if( !visited[i] ) {
verNum = 0;
chk = 1;
dfs2(i);
if( verNum == 1 ){
chk = 1;
continue;
}
if( !chk ) break;
}
}
if(chk) printf("Cactus\n");
else printf("Not cactus\n");
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4040 KB |
Output is correct |
2 |
Correct |
0 ms |
4040 KB |
Output is correct |
3 |
Correct |
0 ms |
4188 KB |
Output is correct |
4 |
Correct |
0 ms |
4072 KB |
Output is correct |
5 |
Correct |
0 ms |
4040 KB |
Output is correct |
6 |
Correct |
4 ms |
4172 KB |
Output is correct |
7 |
Correct |
4 ms |
4172 KB |
Output is correct |
8 |
Correct |
4 ms |
4172 KB |
Output is correct |
9 |
Correct |
0 ms |
4172 KB |
Output is correct |
10 |
Correct |
36 ms |
7340 KB |
Output is correct |
11 |
Correct |
28 ms |
7560 KB |
Output is correct |
12 |
Correct |
44 ms |
9720 KB |
Output is correct |
13 |
Correct |
48 ms |
8792 KB |
Output is correct |
14 |
Correct |
48 ms |
8808 KB |
Output is correct |
15 |
Correct |
52 ms |
10908 KB |
Output is correct |
16 |
Correct |
40 ms |
8452 KB |
Output is correct |
17 |
Correct |
36 ms |
9924 KB |
Output is correct |
18 |
Correct |
52 ms |
12692 KB |
Output is correct |
19 |
Correct |
80 ms |
13828 KB |
Output is correct |
20 |
Correct |
32 ms |
9032 KB |
Output is correct |
21 |
Correct |
72 ms |
7924 KB |
Output is correct |
22 |
Correct |
48 ms |
11512 KB |
Output is correct |
23 |
Correct |
0 ms |
4040 KB |
Output is correct |
24 |
Correct |
0 ms |
4040 KB |
Output is correct |
25 |
Correct |
4 ms |
4772 KB |
Output is correct |
26 |
Correct |
56 ms |
6812 KB |
Output is correct |
27 |
Correct |
20 ms |
5360 KB |
Output is correct |
28 |
Correct |
40 ms |
6416 KB |
Output is correct |
29 |
Correct |
56 ms |
6548 KB |
Output is correct |
30 |
Correct |
40 ms |
6944 KB |
Output is correct |
31 |
Correct |
44 ms |
6416 KB |
Output is correct |
32 |
Correct |
52 ms |
6812 KB |
Output is correct |
33 |
Correct |
56 ms |
6812 KB |
Output is correct |
34 |
Correct |
52 ms |
6812 KB |
Output is correct |