Codeforces Round #383 (Div. 1) C(二分图)
一道很巧妙的二分图的题目
简单分析性质可知,一个合法序列一定是由12,21这样的子串构成的,所以相邻的每隔2个两两配对
然后BF和GF互相配对,思考一下,如果存在奇环,那么必定有一个BG有两个GF,或者一个GF有两个BF,所以不存在这种情况,一定有解
直接二分图判断即可
#include <iostream> #include <cstdio> #include <vector> #define mp make_pair #define fi first #define se second using namespace std; const int maxn = 200050; vector<int> G[maxn]; int color[maxn]; vector <pair <int, int>> Q; void dfs(int x) { for(auto to : G[x]) { if(!color[to]) { color[to] = color[x] == 1 ? 2 : 1; dfs(to); } } } int n, x, y; int main() { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d %d", &x, &y); x--; y--; G[x].push_back(y); G[y].push_back(x); Q.push_back(mp(x, y)); } for(int i = 0; i < n; i ++) G[2*i].push_back(2*i+1), G[2*i+1].push_back(2*i); for(int i = 0; i < 2*n; i++) if(!color[i]) { color[i] = 1; dfs(i); } for(auto x : Q) printf("%d %d\n", color[x.fi], color[x.se]); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步