12745 Wishmaster
view code#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef long long ll; const int N = 200010; const int M = N<<1; int _, cas=1, n, m, pre[N], S[N], c; bool mark[N]; struct edge { int u, v, next; edge() {} edge(int u, int v, int next):u(u),v(v),next(next) {} }e[M]; int ecnt = 0; void addedge(int u, int v) { if(u<0) u = -u+n; if(v<0) v = -v+n; e[ecnt] = edge(u, v, pre[u]); pre[u] = ecnt++; } bool dfs(int x) { int ano = x>n?x-n:x+n; if(mark[ano]) return false; if(mark[x]) return true; mark[x] = true; S[c++] = x; for(int i=pre[x]; ~i; i=e[i].next) { int v = e[i].v; if(!dfs(v)) return false; } return 1; } bool check() { for(int i=1; i<=n; i++) { if(!mark[i] && !mark[i+n]) { c = 0; if(!dfs(i)){ while(c>0) mark[S[--c]] = 0; if(!dfs(i+n)) return 0; } } } return 1; } void solve() { scanf("%d%d", &n, &m); int u, v; ecnt = 0; memset(pre, -1, sizeof(pre)); for(int i=0; i<m; i++) { scanf("%d%d", &u, &v); addedge(-u, v); addedge(-v, u); } memset(mark, 0, sizeof(mark)); printf("Case %d: ", cas++); if(check()) puts("Yes"); else puts("No"); } int main() { // freopen("in.txt", "r", stdin); cin>>_; while(_--) solve(); return 0; }
时间: 2024-12-17 16:25:23