Submission #3567

# Submission time Handle Problem Language Result Execution time Memory
3567 2013-08-31T06:37:35 Z tncks0121 Cactus? Not cactus? (kriii1_C) C++
1 / 1
112 ms 13380 KB
#include <stdio.h>
#include <vector>
#include <stack>
using namespace std;

vector<int> G[100010],C[100010]; int Q[100010]; bool chk[100010];
int N,M;

void in(int x, int y)
{
	C[x].push_back(y);
	C[y].push_back(x);
}

int Depth[100010],Low[100010];
stack<pair<int, int> > S;

int main()
{
	int i,j,x,y;

	scanf ("%d %d",&N,&M);
	for (i=0;i<M;i++){
		scanf ("%d %d",&x,&y);
		G[x].push_back(y);
		G[y].push_back(x);
	}

	S.push(make_pair(1,-1)); Depth[1] = Low[1] = 1;
	while (!S.empty()){
		x = S.top().first; i = S.top().second; S.pop();

		if (i >= 0){
			if (Low[x] > Low[G[x][i]])
				Low[x] = Low[G[x][i]];
		}
		
		for (i++;i<G[x].size();i++){
			y = G[x][i];
			if (Depth[y] == 0){
				Depth[y] = Low[y] = Depth[x] + 1;
				S.push(make_pair(x,i));
				S.push(make_pair(y,-1));
				break;
			}
			else{
				if (Depth[y] + 1 < Depth[x]){
					in(x,y);
					if (Low[x] > Depth[y])
						Low[x] = Depth[y];
				}
			}
		}

		if (i == G[x].size() && !S.empty()){
			if (Low[x] <= Depth[S.top().first]){
				in(x,S.top().first);
			}
		}
	}

	for (i=1;i<=N;i++) if (!chk[i]){
		int head = -1, tail = -1;
		int V = 0, E = 0;

		Q[++head] = i; chk[i] = 1;
		while (tail < head){
			x = Q[++tail];
			V++; E += C[x].size();
			for (j=0;j<C[x].size();j++){
				y = C[x][j];
				if (!chk[y]){
					Q[++head] = y; chk[y] = 1;
				}
			}
		}

		if (V * 2 < E){puts("Not cactus"); return 0;}
	}
	puts("Cactus");

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 7200 KB Output is correct
2 Correct 0 ms 7200 KB Output is correct
3 Correct 4 ms 7464 KB Output is correct
4 Correct 4 ms 7328 KB Output is correct
5 Correct 0 ms 7328 KB Output is correct
6 Correct 0 ms 7332 KB Output is correct
7 Correct 0 ms 7464 KB Output is correct
8 Correct 4 ms 7332 KB Output is correct
9 Correct 4 ms 7464 KB Output is correct
10 Correct 32 ms 10104 KB Output is correct
11 Correct 24 ms 9952 KB Output is correct
12 Correct 44 ms 11012 KB Output is correct
13 Correct 56 ms 11408 KB Output is correct
14 Correct 56 ms 10880 KB Output is correct
15 Correct 68 ms 12068 KB Output is correct
16 Correct 56 ms 11148 KB Output is correct
17 Correct 44 ms 10760 KB Output is correct
18 Correct 84 ms 12852 KB Output is correct
19 Correct 112 ms 13380 KB Output is correct
20 Correct 56 ms 10756 KB Output is correct
21 Correct 84 ms 12848 KB Output is correct
22 Correct 80 ms 12204 KB Output is correct
23 Correct 0 ms 7200 KB Output is correct
24 Correct 0 ms 7200 KB Output is correct
25 Correct 4 ms 7728 KB Output is correct
26 Correct 92 ms 12724 KB Output is correct
27 Correct 32 ms 9424 KB Output is correct
28 Correct 80 ms 12076 KB Output is correct
29 Correct 76 ms 12052 KB Output is correct
30 Correct 44 ms 9708 KB Output is correct
31 Correct 64 ms 12068 KB Output is correct
32 Correct 80 ms 12712 KB Output is correct
33 Correct 88 ms 12712 KB Output is correct
34 Correct 88 ms 12724 KB Output is correct