标签:length alt init mos atom lin blog least topsort
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1127 Accepted Submission(s): 325
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) #define pb push_back #define mp make_pair #define inf 0x3f3f3f3f #define qwer 2e18 using namespace std; typedef long long ll; const int N = 6e5+50; const int M = 16000009; const int mod = 998244353; const double pi= acos(-1.0); typedef pair<int,int>pii; int n,s; int vis[N],in[N]; ll ans[2]; vector<pii>edg[N]; ll topSort(){ queue<int>q; ll ret=1; for(int i=n+1;i<=n+n;i++){ if(in[i]==1){ q.push(i); vis[i]=1; } } while(!q.empty()){ int u=q.front(); q.pop(); for(int i=0;i<edg[u].size();i++){ int v=edg[u][i].first; if(vis[v])continue; if((--in[v])==1)q.push(v),vis[v]=1; if(u>n)ret=(ret*1LL*edg[u][i].second)%mod; } } return ret; } void dfs(int u,int ty,int fa){ vis[u]=1; for(int i=0;i<edg[u].size();i++){ int v=edg[u][i].first; if(v==s&&v!=fa)ans[ty]=(ans[ty]*1LL*edg[u][i].second)%mod; if(vis[v])continue; ans[ty]=(ans[ty]*1LL*edg[u][i].second)%mod; dfs(v,ty^1,u); } } int main(){ //freopen("de.txt","r",stdin); int T; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i=0;i<N;i++)vis[i]=in[i]=0,edg[i].clear(); for(int i=1,v1,w1,v2,w2;i<=n;i++){ scanf("%d%d%d%d",&v1,&w1,&v2,&w2); v1+=n;v2+=n; edg[i].pb(mp(v1,w1)); edg[v1].pb(mp(i,w1)); edg[i].pb(mp(v2,w2)); edg[v2].pb(mp(i,w2)); in[i]+=2; in[v1]++;in[v2]++; } ll anss=topSort(); for(s=1;s<=n;s++){ if(!vis[s]){ ans[0]=ans[1]=1; dfs(s,0,0); anss=anss*((ans[0]+ans[1])%mod)%mod; } } printf("%lld\n",anss); } return 0; }
HDU 6073 Matching In Multiplication(拓扑排序)
标签:length alt init mos atom lin blog least topsort
原文地址:http://www.cnblogs.com/jianrenfang/p/7289552.html