1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10; const double pi = 3.1415926535897932384626433832795; const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++) #define scan(x) scanf("%d", &x) #define mp make_pair #define pb push_back #define lowbit(x) (x&(-x))
#define fi first #define se second #define SZ(x) int((x).size()) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi;
using ll=long long; const int N=3e3+9; int tc,n,m1,m2,s,t,ss,tt,hd[N],cur[N],flow[N],dep[N],sc[N],tot,totp,sum; struct Edge{ int to,nxt,v; }e[4*N]; inline void add(int u,int v,int w){ e[++tot]={v,hd[u],w},hd[u]=tot; e[++tot]={u,hd[v],0},hd[v]=tot; }
bool bfs(int st,int en){ for(int i=0;i<=totp;++i)dep[i]=0; queue<int>q; memcpy(cur,hd,sizeof(hd)); q.push(st); dep[st]=1; while(!q.empty()){ auto u=q.front(); q.pop(); for(int eg=hd[u];eg;eg=e[eg].nxt){ if(!dep[e[eg].to]&&e[eg].v>0){ dep[e[eg].to]=dep[u]+1; q.push(e[eg].to); } } } return !!dep[en]; }
int dfs(int u,int en,int flow){ if(u==en)return flow; int r=flow; for(int eg=cur[u];eg&&r;eg=e[eg].nxt){ cur[u]=eg; if(e[eg].v>0&&dep[e[eg].to]==dep[u]+1){ int c=dfs(e[eg].to,en,min(r,e[eg].v)); r-=c; e[eg].v-=c,e[eg^1].v+=c; } } return flow-r; }
int dinic(int st,int en){ int ret=0; while(bfs(st,en)){ ret+=dfs(st,en,0x3f3f3f3f); } return ret; }
bool solve(){ tot=1,sum=0; cin>>n>>m1>>m2; s=n+m2+1,t=s+1,tt=t+1,totp=tt; for(int i=0;i<=totp;++i)hd[i]=0,sc[i]=0,flow[i]=0; for(int i=1,x,y,z;i<=m1;++i){ cin>>x>>y>>z; if(z==1)sc[x]+=1; else sc[y]+=1; } for(int i=1,x,y;i<=m2;++i){ cin>>x>>y; if(x==1||y==1)sc[1]+=1; else{ add(s,n+i,1); flow[s]-=1,flow[n+i]+=1; add(n+i,x,1),add(n+i,y,1); } } for(int i=2;i<=n;++i)if(sc[i]>sc[1])return false; for(int i=2;i<=n;++i){ add(i,t,sc[1]-sc[i]); } for(int i=n+1;i<tt;++i){ if(flow[i]>0){ sum+=flow[i]; add(ss,i,flow[i]); }else if(flow[i]<0)add(i,tt,-flow[i]); } add(t,s,0x3f3f3f3f); return sum==dinic(ss,tt); }
int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>tc; f(sb,1,tc){ cout<<(solve()?"YES\n":"NO\n"); } return 0; }
|