#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <set> #define LL long long #define FOR(i, x, y) for(int i=x;i<=y;i++) using namespace std; const int MAXN = 100 + 10; struct Edge { int to; int next; }edge[MAXN*MAXN]; int head[MAXN]; int G[MAXN][MAXN]; int col[MAXN]; int tot; void AddEdge(int u, int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } void init() { tot = 0; memset(head, -1, sizeof(head)); } bool dfs(int u, int c) { col[u] = c; for(int i=head[u];i!=-1;i=edge[i].next) { int v = edge[i].to; if(col[v] != -1) { if(col[v] == c) return false; } else if(!dfs(v, col[u]^1)) return false; } return true; } int main() { int n; while(scanf("%d", &n)!=EOF && n) { memset(G, 0, sizeof(G)); int t; FOR(i, 1, n) { while(scanf("%d", &t) && t) { G[i][t] = 1; } } init(); FOR(i, 1, n) { FOR(j, i + 1, n) { if(G[i][j] == 0 || G[j][i] == 0) { AddEdge(i, j); AddEdge(j, i); } } } memset(col, -1, sizeof(col)); int flag = 1; for(int i=1;i<=n;i++) { if(col[i] == -1 && dfs(i, 0) == false) { flag = 0; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
时间: 2024-10-17 07:03:49