【题解】CF742E (二分图+构造)

【题解】CF742E (二分图+构造)

自闭了CodeForces - 742E

给定的条件就是一个二分图的模型,但是有一些不同。不同就不同在可以出现相邻两个点颜色相同的情况。

构造常用方法之一是按奇偶分类,就是尽管不同奇偶性的块之间会产生影响,但是我们先不管这些限制。

这道题里,假若奇偶块之内都能满足题目的限制,那么奇偶块之间也满足限制了。因为限制是不能存在三个联系相等,然而我们已经保证块内每两个不相等。

  • 男女朋友连边。
  • \(2i\)\(2i-1\)连边。

然后跑二分图染色,考虑是否存在无解,这个图显然有染色方案,因为所有联通块个数为偶数

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define ERR(s) cerr<<(#s)<<"="<<(s)<<endl;
#define getchar() (__c==__ed?(__ed=__buf+fread(__c=__buf,1,19260817,stdin),*__c++):*__c++)


using namespace std;  typedef long long ll;   char __buf[19260817],*__c=__buf,*__ed=__buf; 
inline int qr(){
      register int ret=0,f=0;
      register char c=getchar();
      while(!isdigit(c))f|=c==45,c=getchar();
      while(isdigit(c)) ret=ret*10+c-48,c=getchar();
      return f?-ret:ret;
}
const int maxn=2e5+5;
vector<int> e[maxn];
inline void add(const int&fr,const int&to){e[fr].push_back(to); e[to].push_back(fr);}
pair<int,int> P[maxn>>1];
int n,w[maxn];
queue<int> q;
inline void bfs(const int&s){
      q.push(s);
      if(w[s-1]) w[s]=w[s-1]^1;
      else w[s]=2;
      while(q.size()){
	    int now=q.front();
	    q.pop();
	    for(auto t:e[now])
		  if(!w[t]) w[t]=w[now]^1,q.push(t);
      }
}

int main(){
      n=qr();
      for(int t=1;t<=n;++t) P[t].first=qr(),P[t].second=qr(),add(P[t].first,P[t].second);
      for(int t=1;t<=n+n;t+=2) add(t,t+1);
      for(int t=1;t<=n+n;++t) if(!w[t]) bfs(t);
      for(int t=1;t<=n;++t)
	    printf("%d %d\n",w[P[t].first]-1,w[P[t].second]-1);      
      return 0;
}


后话:博主最开始看错题了(英语太菜),把题干里的some solution scheme看作some solution schemes,加上没看样例和输出格式。以为要计数,想了很久不会做。有没有人会做这题的计数版本?就是问有多少种合法方案。

posted @ 2019-10-26 11:23  谁是鸽王  阅读(369)  评论(0编辑  收藏  举报