#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <set>
int n,top2=-1,stack2[200001],stack[200001],top=-1,save[200001],cnt[200001];
using namespace std;
vector<int> list[200001],list2[200001];
set< pair<int,int> > edge;
bool check[100001],flag,backedge;
void back(int k){
int i;
stack[++top]=k;
while(top!=-1){
k=stack[top];
check[k]=1;
for(;save[k]<list[k].size();save[k]++){
if(!check[list[k][save[k]]]){
list2[list[k][save[k]]].push_back(k);
edge.insert(make_pair(list[k][save[k]],k));
stack[++top]=list[k][save[k]];
break;
}
else if(edge.find(make_pair(k,list[k][save[k]]))==edge.end()){
edge.insert(make_pair(list[k][save[k]],k));
cnt[list[k][save[k]]]++;
list2[list[k][save[k]]].push_back(k);
}
}
if(save[k]==list[k].size()){
top--;
stack2[++top2]=k;
}
save[k]++;
}
}
void flood(int k){
int i,sum=0;
stack[++top]=k;
while(top!=-1){
k=stack[top];
if(!check[k])
sum+=cnt[k];
check[k]=1;
for(;save[k]<list2[k].size();save[k]++){
if(!check[list2[k][save[k]]]){
stack[++top]=list2[k][save[k]];
break;
}
}
if(save[k]==list2[k].size())
top--;
}
if(sum>=2)
flag=1;
}
int main(){
int m,i,a,b;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++){
scanf("%d %d",&a,&b);
a--;b--;
list[a].push_back(b);
list[b].push_back(a);
}
for(i=0;i<n;i++){
if(!check[i])
back(i);
}
for(i=0;i<n;i++){
check[i]=0;save[i]=0;
}
for(;top2>=0;top2--){
backedge=0;
if(!check[stack2[top2]])
flood(stack2[top2]);
}
if(flag)
printf("Not cactus\n");
else
printf("Cactus\n");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13808 KB |
Output is correct |
2 |
Correct |
0 ms |
13808 KB |
Output is correct |
3 |
Correct |
4 ms |
14204 KB |
Output is correct |
4 |
Correct |
4 ms |
14204 KB |
Output is correct |
5 |
Correct |
4 ms |
14072 KB |
Output is correct |
6 |
Correct |
4 ms |
14336 KB |
Output is correct |
7 |
Correct |
4 ms |
14336 KB |
Output is correct |
8 |
Correct |
4 ms |
14204 KB |
Output is correct |
9 |
Correct |
0 ms |
14336 KB |
Output is correct |
10 |
Correct |
116 ms |
23048 KB |
Output is correct |
11 |
Correct |
92 ms |
21848 KB |
Output is correct |
12 |
Correct |
100 ms |
20540 KB |
Output is correct |
13 |
Correct |
120 ms |
21728 KB |
Output is correct |
14 |
Correct |
128 ms |
21464 KB |
Output is correct |
15 |
Correct |
124 ms |
22256 KB |
Output is correct |
16 |
Correct |
100 ms |
20672 KB |
Output is correct |
17 |
Correct |
88 ms |
20144 KB |
Output is correct |
18 |
Correct |
172 ms |
23840 KB |
Output is correct |
19 |
Correct |
180 ms |
24632 KB |
Output is correct |
20 |
Correct |
100 ms |
20808 KB |
Output is correct |
21 |
Correct |
168 ms |
23840 KB |
Output is correct |
22 |
Correct |
140 ms |
22652 KB |
Output is correct |
23 |
Correct |
0 ms |
13940 KB |
Output is correct |
24 |
Correct |
0 ms |
13808 KB |
Output is correct |
25 |
Correct |
4 ms |
14864 KB |
Output is correct |
26 |
Correct |
136 ms |
23444 KB |
Output is correct |
27 |
Correct |
64 ms |
18164 KB |
Output is correct |
28 |
Correct |
116 ms |
22256 KB |
Output is correct |
29 |
Correct |
132 ms |
22652 KB |
Output is correct |
30 |
Correct |
124 ms |
22652 KB |
Output is correct |
31 |
Correct |
136 ms |
22256 KB |
Output is correct |
32 |
Correct |
148 ms |
23444 KB |
Output is correct |
33 |
Correct |
148 ms |
23444 KB |
Output is correct |
34 |
Correct |
156 ms |
23444 KB |
Output is correct |