首页 > 代码库 > 15 day 1

15 day 1

第一题

用堆维护。

#include <cstdio>#include <algorithm>#include <queue>int n,i,f[400000],g[2][200000],j=0,k[400000];int l,r;bool cho;struct pn{	int l,r,n;};bool operator<(pn a,pn b){	return a.n>b.n;}std::priority_queue<pn> q;pn as,as1,as2;int main(){	freopen("minval.in","r",stdin);	freopen("minval.out","w",stdout);	scanf("%d",&n);	for(i=0;i<n;++i) scanf("%d",&g[0][i]);	for(i=0;i<n;++i) scanf("%d",&g[1][i]);	std::sort(g[0],g[0]+n);	std::sort(g[1],g[1]+n);	as.l=0;	as.r=0;	as.n=g[0][0]+g[1][0];	q.push(as);	for(i=0;i<n;++i){		as=q.top();		q.pop();		as1.l=as.l+1;		as1.r=as.r;		as1.n=g[0][as1.l]+g[1][as1.r];		as2.l=as.l;		as2.r=as.r+1;		as2.n=g[0][as2.l]+g[1][as2.r];		printf("%d ",as.n);		q.push(as1);		q.push(as2);	}	return 0;}

第二题

连通性判定+二分图

#include <cstdio>#include <cstring>int f[200000],d,i,j,m,n,s,t,tt,a,b,pl,h[200000];int q[200000],qh,qt;bool f2;struct edge{	int t,n;} edges[2000000];inline void addedge(int f,int t){	edges[++pl].n=h[f];	edges[ pl ].t= t  ;	h[f]=pl;} int main(){	freopen("catch.in","r",stdin);	freopen("catch.out","w",stdout);	scanf("%d",&tt);	while(tt--){		++j;		memset(f,0,sizeof f);		memset(h,0,sizeof h);		pl=0;		qh=qt=0;		scanf("%d%d%d",&n,&m,&s);		for(i=0;i<m;++i){			scanf("%d%d",&a,&b);			addedge(a,b);			addedge(b,a);		}		q[qt++]=s;		f2=false;		f[s]=1;		--n;		while(qh!=qt){			i=q[qh++];			for(d=h[i];d;d=edges[d].n){				t=edges[d].t;				if(!f[t]){					f[t]=3-f[i];					q[qt++]=t;					--n;				}else{					if(f[i]==f[t]) f2=true;//not bicolorable				}			}		}		if(!f2){			printf("Case %d: NO\n",j);		}else{			if(n) printf("Case %d: NO\n",j); else printf("Case %d: YES\n",j);		}	}	return 0;}

  

15 day 1