树上差分 WA on #15 求助
  • 板块CF19E Fairy
  • 楼主Little_CartG2_leaf
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/11/6 14:21
  • 上次更新2024/11/6 15:15:46
查看原帖
树上差分 WA on #15 求助
392157
Little_CartG2_leaf楼主2024/11/6 14:21

rt

思路大体和第一篇题解相同。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mkp make_pair
const int N=300024;
int t,n,m;
int fa[N],Fa[N],dep[N],a[N];
vector<PII> e[N];
vector<PII> b;
vector<int> ans;
void dfs0(int u){
	for(auto tp:e[u]){
		int v=tp.fi;
		if(dep[v]){
			continue;
		}
		dep[v]=dep[u]+1;
		fa[v]=u;
		Fa[v]=tp.se;
		dfs0(v);
	}
}
int cnt,tot;
void dfs1(int u){
	for(auto tp:e[u]){
		int v=tp.fi;
		if(Fa[v]!=tp.se){
			continue;
		}
		dfs1(v);
		if(a[v]==tot){
			ans.push_back(Fa[v]);
		}
		a[u]+=a[v];
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		e[x].push_back(mkp(y,i));
		e[y].push_back(mkp(x,i));
		b.push_back(mkp(x,y));
	}
	dep[1]=1;
	dfs0(1);
	int cur=0,idx;
	for(PII tp:b){
		cur++;
		int x,y;
		x=tp.fi;
		y=tp.se;
		if(dep[x]<dep[y]) swap(x,y);
		if(fa[x]==y){
			continue;
		}
		if((dep[y]-dep[x]+1)&1){
			tot++;
			idx=cur;
			a[x]++;
			a[y]--; 
		}
		else{
			a[x]--;
			a[y]++; 
		}
	}
	if(tot==0){
		cout<<m<<"\n";
		for(int i=1;i<=m;i++){
			cout<<i<<" ";
		}
		return 0;
	}
	if(tot==1){
		ans.push_back(idx);
	}
	dfs1(1);
	cout<<ans.size()<<"\n";
	sort(ans.begin(),ans.end());
	for(int x:ans){
		cout<<x<<" ";
	}
	return 0;
}

2024/11/6 14:21
加载中...