标签:des style blog http color os io strong ar
2
1
2 512
2
1 2
2 1
YES
512
一道很裸的tarjan题,但明显我的tarjan学了跟没学一样,很多小细节没有注意(更新low什么时候用dfn,什么时候用low;tarjan完了要把点从图中删除)。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; #define MAXN 10000 #define MAXE MAXN * 2 #define MAXV MAXN #define INF 0x3f3f3f3f int n,m; struct Edge { int np; Edge *next; }E[MAXE],*V[MAXV]; int tope=-1; void addedge(int x,int y) { E[++tope].np=y; E[tope].next=V[x]; V[x]=&E[tope]; } int state[MAXN]; int value[MAXN]; int dfn[MAXN],low[MAXN],dfstime=0; int stack[MAXN],tops=-1; int color[MAXN],totc=0; void tarjan(int now) { low[now]=dfn[now]=++dfstime; Edge *ne; stack[++tops]=now; for (ne=V[now];ne;ne=ne->next) { if (dfn[ne->np])//state[ne->np]==1 { if (color[ne->np])continue; low[now]=min(low[now],dfn[ne->np]); }else { tarjan(ne->np); low[now]=min(low[now],low[ne->np]);//易写反 } } if (low[now]==dfn[now]) { totc++; while (stack[tops]!=now) { color[stack[tops--]]=totc; } color[stack[tops--]]=totc; } } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); int t; int x,y; int i,j,k; scanf("%d",&n); memset(value,INF,sizeof(value)); scanf("%d",&t); for (i=0;i<t;i++) { scanf("%d%d",&x,&y); value[x]=y; } scanf("%d",&m); for (i=0;i<m;i++) { scanf("%d%d",&x,&y); addedge(x,y); } for (i=1;i<=n;i++) { if (!dfn[i]) { tarjan(i); } } Edge *ne; for (i=1;i<=n;i++) { for (ne=V[i];ne;ne=ne->next) { if (color[ne->np]==color[i])continue; state[color[ne->np]]=true; } } x=INF; bool ok=true; int ans=0; int ans2=INF; for (i=1;i<=totc;i++) { if (state[i])continue; x=INF; for (j=1;j<=n;j++) { if (color[j]!=i)continue; x=min(x,value[j]); } if (x==INF) { ok=false; for (j=1;j<=n;j++) { if (color[j]==i) { ans2=min(ans2,j); break; } } } ans+=x; } if (!ok) { printf("NO\n%d\n",ans2); }else { printf("YES\n%d\n",ans); } }
v
标签:des style blog http color os io strong ar
原文地址:http://www.cnblogs.com/mhy12345/p/3948357.html