将一个点 \((x,y)\) 定义为 \(x\)\(y\) 连的一条无向边,将问题转化为求欧拉路径,这样入度减出度必然 \(\leq 1\),这样还是不太好做,再转化一步,将每个点向一个虚拟点连一条边,那么我们就可以发现这样做只需要跑一个欧拉回路即可,因为度数为奇数的点也即欧拉路径的起点和终点一定是偶数个。
代码实现如下

#include<bits/stdc++.h>
#define RG register
#define LL long long
#define U(x, y, z) for(RG int x = y; x <= z; ++x)
#define D(x, y, z) for(RG int x = y; x >= z; --x)
using namespace std;
template <typename T> void read(T &n){ bool f = 1; char ch = getchar(); n = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = 0; for (; isdigit(ch); ch = getchar()) n = (n << 1) + (n << 3) + (ch ^ 48); if (f == 0) n = -n;}
inline char Getchar(){ char ch; for (ch = getchar(); !isalpha(ch); ch = getchar()); return ch;}
template <typename T> inline void write(T n){ char ch[60]; bool f = 1; int cnt = 0; if (n < 0) f = 0, n = -n; do{ch[++cnt] = char(n % 10 + 48); n /= 10; }while(n); if (f == 0) putchar('-'); for (; cnt; cnt--) putchar(ch[cnt]);}
template <typename T> inline void writeln(T n){write(n); putchar('\n');}
template <typename T> inline void writesp(T n){write(n); putchar(' ');}
template <typename T> inline void chkmin(T &x, T y){x = x < y ? x : y;}
template <typename T> inline void chkmax(T &x, T y){x = x > y ? x : y;}
template <typename T> inline T Min(T x, T y){return x < y ? x : y;}
template <typename T> inline T Max(T x, T y){return x > y ? x : y;}
inline void readstr(string &s) { s = ""; static char c = getchar(); while (isspace(c)) c = getchar(); while (!isspace(c)) s = s + c, c = getchar();}
inline void FO(string s){freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout);}

const int N = 4e5 + 10;
int n;
int head[N], cnt = 1, flg[N << 1], ans[N], deg[N], tag;
struct edge{
	int id, v, nxt;
}e[N << 1];

inline void aedge(int id, int u, int v) {
	e[++cnt] = (edge) {id, v, head[u]}; head[u] = cnt;
}

inline void dfs(int u) {
	for (int &i = head[u]; i; i = e[i].nxt) {
		if (flg[i]) continue ;
		flg[i] = flg[i ^ 1] = 1;
		ans[e[i].id] = tag;
		tag ^= 1;
		dfs(e[i].v);
		
	}
}

int main(){
	//FO("");
	read(n);
	U(i, 1, n) {
		int u, v;
		read(u), read(v);
		v += 2e5;
		deg[u]++, deg[v]++;
		aedge(i, u, v), aedge(i, v, u);
	}

	U(i, 1, N - 10) if (deg[i] & 1) aedge(0, i, 0), aedge(0, 0, i);
	U(i, 0, N - 10) dfs(i);
	U(i, 1, n) if (ans[i]) putchar('b'); else putchar('r');
	return 0;
}

原文地址:http://www.cnblogs.com/SouthernWay/p/16852857.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性