Esempio n. 1
0
int main()
{
	int runs, n, m;
	scanf("%d", &runs);
	while (runs--) {
		nA = 0;
		memset(V, 0, sizeof(V));
		scanf("%d%d", &n, &m);
		for (int i = 0, a, b; i < m; ++i) {
			scanf("%d%d", &a, &b);
			addArc(a, b);
		}
		init_tarjan();
		for (int i = 1; i <= n; ++i) {
			if (vis[i] == 0) {
				tarjan(i);
			}
		}
		memset(mat, 0, sizeof(mat));
		memset(ind, 0, sizeof(ind));
		for (int i = 1; i <= n; ++i) {
			for (Arc *a = V[i]; a; a = a->next) {
				int u = id[i], v = id[a->to];
				if (u != v && false == mat[u][v]) {
					mat[u][v] = true;
					++ind[v];
				}
			}
		}
		// top sort
		top = -1;
		for (int i = 0; i < sc_cnt; ++i) {
			if (ind[i] == 0) vstack[++top] = i;
		}
		int cnt = 0;
		while (cnt < sc_cnt) {
			int u = vstack[top--];
			ord[cnt++] = u;
			for (int v = 0; v < sc_cnt; ++v) {
				if (mat[u][v]) {
					--ind[v];
					if (ind[v] == 0) vstack[++top] = v;
				}
			}
		}
		bool ans = true;
		for (int i = 0; i < cnt-1; ++i) {
			if (false == mat[ord[i]][ord[i+1]]) {
				ans = false;
				break;
			}
		}
		puts(ans ? "Yes" : "No");
	}
	return 0;
}
Esempio n. 2
0
int tarjan(int n){
    init_tarjan();
    for(int i=0;i<n;i++){
        if(!pre[i]) dfs(i);
    }
    for(int i=0;i<n;i+=2){
//        printf("%d   %d\n",subid[i],subid[i^1]);
        if(subid[i]==subid[i^1]) return 0;
    }
    return 1;
}