欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

2021牛客多校6 - Defend Your Country(点双缩点求割点)

发布时间:2024/4/11 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 2021牛客多校6 - Defend Your Country(点双缩点求割点) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接:点击查看

题目大意:给出一个 nnn 个点和 mmm 条边组成的无向图,每个点都有点权,现在可以删除掉任意条边,问如何才能使得贡献和最大

贡献和是指,当删掉边后,对于每个连通块而言,如果连通块内点数为奇数,那么贡献和减去点权和,如果连通块内点数为偶数,那么贡献和加上点权和

题目分析:因为题目保证了是连通图,所以当 nnn 为偶数时显然输出所有点的点权之和是最优的,当 nnn 为奇数时,只需要删掉一个点一定是最优的

证明里的圆方树涉及到了我的知识盲点,过两天会去学的。。所以对于本题而言只能大力猜结论了

实现的话,对于某个点而言,如果不是割点的话,删掉的话对原图无影响,如果是割点的话,需要判断一下其子树是否全部为偶数才行,这个在 tarjan 处理的过程中维护一下子树的大小就可以了

代码:

// Problem: Defend Your Country // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/11257/J // Memory Limit: 2097152 MB // Time Limit: 10000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; const int M=2e6+100; struct Egde {int to,next; }edge1[M]; int head1[N],low[N],dfn[N],a[N],sz[N],num,cnt1,root,n,m; LL sum,ans; void addedge1(int u,int v) {edge1[cnt1].to=v;edge1[cnt1].next=head1[u];head1[u]=cnt1++; } void tarjan(int u) {dfn[u]=low[u]=++num;int flag=0;bool ok=true;sz[u]=1;for(int i=head1[u];i!=-1;i=edge1[i].next){int v=edge1[i].to;if(!dfn[v]){tarjan(v);sz[u]+=sz[v];low[u]=min(low[u],low[v]);if(low[v]>=dfn[u]){flag++;if(u!=root||flag>1) {if(sz[v]&1) {ok=false;}}}}elselow[u]=min(low[u],dfn[v]);}if(ok) {ans=max(ans,sum-2*a[u]);} } void solve() {for(int i=1;i<=n;i++)//找割点+缩点 if(!dfn[i]){root=i;tarjan(i);} } void init() {num=cnt1=root=0;sum=0,ans=-1e18;memset(head1,-1,sizeof(int)*(n+5));memset(low,0,sizeof(int)*(n+5));memset(dfn,0,sizeof(int)*(n+5));memset(sz,0,sizeof(int)*(n+5)); } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {read(n),read(m);init();for(int i=1;i<=n;i++) {read(a[i]);sum+=a[i];}while(m--) {int u,v;read(u),read(v);addedge1(u,v);addedge1(v,u);}if(n%2==0) {cout<<sum<<endl;} else {solve();cout<<ans<<endl;}}return 0; }

总结

以上是生活随笔为你收集整理的2021牛客多校6 - Defend Your Country(点双缩点求割点)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。