#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <stack>
using namespace std;
vector<int> node[100001];
vector<int> rv[100001];
bool chk[100001];
int group[100001];
int cnt[100001];
stack<int> st;
int ret;
int grp;
void dfs(int x)
{
chk[x] = true;
for(int i = 0 ;i < node[x].size();i++)
{
if( !chk[node[x][i]] )
dfs(node[x][i]);
}
st.push(x);
}
void dfs2(int x)
{
chk[x] = true;
group[x] = grp;
for(int i = 0 ;i < rv[x].size();i++)
{
if( !chk[rv[x][i]] ) {
cnt[rv[x][i]]++;
dfs2(rv[x][i]);
}
else if( group[rv[x][i]] == grp )
{
cnt[rv[x][i]]++;
}
}
}
int main(void){
int n, m;
scanf("%d %d",&n, &m);
for(int i = 0 ;i < m ;i++)
{
int a, b;
scanf("%d %d", &a, &b);
node[a].push_back(b);
rv[b].push_back(a);
}
memset(chk,0,sizeof(chk));
for(int i = 1; i <= n;i++)
{
if( !chk[i] )
dfs(i);
}
memset(cnt,0,sizeof(cnt));
memset(group, 0, sizeof(group));
memset(chk,0,sizeof(chk));
grp = 0;
while(!st.empty()){
int next = st.top();
st.pop();
if(!chk[next])
{
dfs2(next);
grp++;
}
}
for(int i = 1 ;i <=n ;i++)
{
int count = 0;
for(int j = 0 ;j < rv[i].size();j++)
{
if( group[rv[i][j]] == group[i] )
count++;
}
if( count > 1 )
{
printf("Not cactus\n");
return 0;
}
}
printf("Cactus\n");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6804 KB |
Output is correct |
2 |
Correct |
4 ms |
6804 KB |
Output is correct |
3 |
Correct |
4 ms |
6936 KB |
Output is correct |
4 |
Correct |
0 ms |
6936 KB |
Output is correct |
5 |
Correct |
4 ms |
6936 KB |
Output is correct |
6 |
Correct |
4 ms |
6936 KB |
Output is correct |
7 |
Correct |
0 ms |
6936 KB |
Output is correct |
8 |
Correct |
0 ms |
6936 KB |
Output is correct |
9 |
Correct |
4 ms |
6936 KB |
Output is correct |
10 |
Correct |
52 ms |
10100 KB |
Output is correct |
11 |
Correct |
20 ms |
9740 KB |
Output is correct |
12 |
Correct |
40 ms |
9968 KB |
Output is correct |
13 |
Correct |
44 ms |
10368 KB |
Output is correct |
14 |
Correct |
52 ms |
10240 KB |
Output is correct |
15 |
Correct |
44 ms |
10760 KB |
Output is correct |
16 |
Correct |
44 ms |
9972 KB |
Output is correct |
17 |
Correct |
32 ms |
9708 KB |
Output is correct |
18 |
Correct |
60 ms |
11420 KB |
Output is correct |
19 |
Correct |
72 ms |
11816 KB |
Output is correct |
20 |
Correct |
36 ms |
9840 KB |
Output is correct |
21 |
Correct |
52 ms |
11420 KB |
Output is correct |
22 |
Correct |
44 ms |
10892 KB |
Output is correct |
23 |
Incorrect |
0 ms |
6804 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |