标签:
5438 好吉利的题号
不停的删掉度数小于等于1的点并更新各点度数,直至无法删点,然后dfs即可
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define ll __int64 const int maxm=100008; const int maxn=10008; struct fuck{ int u,v,w,next; }edge[maxm<<1]; int head[maxn]; int w[maxn]; int tol; bool ban[maxn]; void init() { tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v) { edge[tol].u=u; edge[tol].v=v; edge[tol].next=head[u]; head[u]=tol++; } int du[maxn]; bool vis[maxn]; int tim; ll dfs(int u) { ban[u]=true; int i,v; tim++; ll sum=w[u]; for(i=head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(ban[v]) continue; sum+=dfs(v); } return sum; } void del(int n) { queue<int> q; memset(vis,false,sizeof(vis)); int u,v,i; for(i=1;i<=n;i++) if(du[i]<=1) { q.push(i); } while(!q.empty()) { u=q.front();q.pop(); ban[u]=true; for(i=head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(ban[v]) continue; du[v]--; if(du[v]<=1) q.push(v); } } } int main() { int i,j,n,m,t,u,v; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); memset(du,0,sizeof(du)); memset(ban,false,sizeof(ban)); for(i=1;i<=n;i++) scanf("%d",&w[i]); for(i=1;i<=m;i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); du[u]++;du[v]++; } del(n); ll ans=0,sum; for(i=1;i<=n;i++) { if(!ban[i]) { tim=0; sum=dfs(i); if(tim%2&&tim>2) ans+=sum; } } printf("%I64d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/bitch1319453/p/4805467.html