CF1186F Vus the Cossack and a Graph

题目

CF1186F Vus the Cossack and a Graph

分析

直接贪心。先定义“需要度数”就是题目中的 \(f\)

策略就是每次在优先队列里面找到当前 \(f\) 最小的点,然后遍历出边,并对这些节点按照当前的 \(f\) 再次排序,选取 \(f[v]\) 较大的 \(f[x]\) 条边(这里假设当前点是 \(x\) ,遍历出去的点是 \(v\)) 。

然后更新这些 \(f[v]\) 后放进优先队列里面,如此循环。

这样做可以保证一定是先满足小的 \(f\) 再去满足大的 \(f\) ,不会出现浪费的情况,具体证明待补。

代码

#include<bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
	#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
	char buf[1<<21],*p1=buf,*p2=buf;
#endif
template<typename T>
inline void read(T &x){
	x=0;bool f=false;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=x*10+(ch^48);ch=getchar();}
	x=f?-x:x;
	return ;
}
template<typename T>
inline void write(T x){
	if(x<0) x=-x,putchar('-');
	if(x>9) write(x/10);
	putchar(x%10^48);
	return ;
}
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pc putchar
#define PII pair<int,int>
#define rep(i,x,y) for(register int i=(x);i<=(y);i++)
#define dep(i,y,x) for(register int i=(y);i>=(x);i--)
const int MOD=1e9+7;
inline int inc(int x,int y){x+=y;return x>=MOD?x-MOD:x;}
inline int dec(int x,int y){x-=y;return x<0?x+MOD:x;}
inline void incc(int &x,int y){x+=y;if(x>=MOD) x-=MOD;}
inline void decc(int &x,int y){x-=y;if(x<0) x+=MOD;}
inline void chkmin(int &x,int y){if(y<x) x=y;}
inline void chkmax(int &x,int y){if(y>x) x=y;}
const int N=1e6+5,M=2e5+5,INF=1e9+7;
int n,m,ind[N],d[N],top,cnt;
PII sta[N],st[N];
int head[N],to[N<<1],fr[N<<1],nex[N<<1],idx=1;
bool vis[N<<1],vi[N];
inline void add(int u,int v){fr[idx]=u;nex[++idx]=head[u];to[idx]=v;head[u]=idx;return ;}
priority_queue<PII,vector<PII>,greater<PII> >pq;
inline bool cmp(const PII &x,const PII &y){return d[x.fi]>d[y.fi];}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
//	ios::sync_with_stdio(false);
	double ST=clock();
	read(n),read(m);int u,v;
	rep(i,1,m) read(u),read(v),add(u,v),add(v,u),ind[u]++,ind[v]++;
	rep(i,1,n) d[i]=(int)ceil(1.0*ind[i]/2.0),pq.push(mp(d[i],i));
	while(!pq.empty()){
		PII t=pq.top();pq.pop();
		int x=t.se,val=t.fi;
		if(vi[x]) continue;
		vi[x]=true;top=0;d[x]=0;
		for(int i=head[x];i;i=nex[i]){
			int y=to[i];
			if(!vis[i]) sta[++top]=mp(y,i);
		}
		sort(sta+1,sta+top+1,cmp);
		for(int i=1;i<=val;i++) d[sta[i].fi]--,pq.push(mp(d[sta[i].fi],sta[i].fi)),vis[sta[i].se]=vis[sta[i].se^1]=true,st[++cnt]=mp(x,sta[i].fi);
	}
	write(cnt),pc('\n');
	rep(i,1,cnt) write(st[i].fi),pc(' '),write(st[i].se),pc('\n');
//	int tmp=(int)ceil((n+m)*1.0/2.0)-cnt;
//	for(int i=2;i<=idx;i++){
//		if(!tmp) break;
//		if(!vis[i]) vis[i]=vis[i^1]=true,tmp--,write(fr[i]),pc(' '),write(to[i]),pc('\n');
//	}
#ifndef ONLINE_JUDGE
	cerr<<"\nTime:"<<(clock()-ST)/CLOCKS_PER_SEC<<"s\n";
#endif
	return 0;
}
/*

*/



posted @ 2021-10-26 20:49  __Anchor  阅读(34)  评论(0编辑  收藏  举报