Codechef:Counting The Important Pairs/TAPAIR

传送门

题解:
一定割一条树边,然后分类讨论即可。

注意两条被环覆盖的树边同时被删除合法当且仅当覆盖他们的环边集合相同,这个可以hash。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int RLEN=1<<18|1;
inline char nc() {
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob) ? -1 : *ib++;
}
inline int rd() {
	char ch=nc(); int i=0,f=1;
	while(!isdigit(ch)) {if(ch=='-')f=-1; ch=nc();}
	while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();}
	return i*f;
}

const int N=1e5+50;
int n,m,a[N<<2],b[N<<2],c[N],ind; LL ans;
int st[N],sze[N],son[N],top[N],fa[N],dep[N];
vector <int> edge[N];
vector <int> e[N];
inline void dfs(int x,int f) {
	sze[x]=1; st[x]=++ind; fa[x]=f; dep[x]=dep[f]+1;
	for(auto v:edge[x]) if(!st[v]) {
		dfs(v,x); sze[x]+=sze[v]; 
		e[x].push_back(v);
		if(sze[son[x]]<sze[v]) son[x]=v;
	}
}
inline void Dfs(int x,int f) {top[x]=f; for(auto v:e[x]) Dfs(v,(son[x]==v) ? f : v);}
inline int lca(int x,int y) {while(top[x]^top[y]) (dep[top[x]]>dep[top[y]]) ? (x=fa[top[x]]) : (y=fa[top[y]]); return (dep[x]<dep[y]) ? x : y;}
inline void dfs2(int x,int f) {for(auto v:e[x]) dfs2(v,x), c[x]+=c[v];}
map <int,int> s;
inline void dfs3(int x,int f) {for(auto v:e[x]) dfs3(v,x), c[x]^=c[v]; if(x!=1) ans+=s[c[x]]++;}
int main() {
	n=rd(), m=rd(); srand(time(0));
	for(int i=1;i<=m;i++) {
		int x=a[i]=rd(), y=b[i]=rd();
		edge[x].push_back(y);
		edge[y].push_back(x);
	} dfs(1,0); Dfs(1,1);
	for(int i=1;i<=m;i++) {
		int x=a[i], y=b[i];
		if(fa[x]==y || fa[y]==x) continue;
		int l=lca(x,y);
		c[l]-=2; c[x]++; c[y]++;
	} dfs2(1,0);
	for(int i=2;i<=n;i++) if(!c[i]) ++ans;
	ans=(LL)ans*(m-ans);
	for(int i=2;i<=n;i++) if(c[i]==1) ++ans;
	for(int i=1;i<=n;i++) c[i]=0;
	for(int i=1;i<=m;i++) {
		int x=a[i], y=b[i];
		if(fa[x]==y || fa[y]==x) continue;
		int l=lca(x,y), t=(rand()<<4)^rand();
		c[x]^=t; c[y]^=t;
	} dfs3(1,0);
	cout<<ans<<'\n';
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值