题目链接
- 题意:
给很多由两个颜色组成的珠子,看是否能连成一串,相邻珠子相邻部分颜色相同 - 分析:
裸地欧拉路判断+路径输出
const int MAXN = 1100;
const int MAXV = 60;
const int MAXE = 2020;
struct Edge
{
int from, to;
};
struct UnDirect_Euler
{
int n, m;
int deg[MAXV];
bool vis[MAXE];
vector<int> G[MAXV];
vector<Edge> edges;
stack<int> sk;
vector<int> ans;
void init(int n)
{
this->n = n;
edges.clear();
REP(i, n)
{
deg[i] = 0;
G[i].clear();
}
}
void addEdge(int a, int b)
{
edges.push_back((Edge) { a, b });
edges.push_back((Edge) { b, a });
m = edges.size();
G[a].push_back(m - 2);
G[b].push_back(m - 1);
deg[a]++;
deg[b]++;
}
void dfs(int u, int ind)
{
REP(i, G[u].size())
{
int x = G[u][i];
Edge& e = edges[x];
if (!vis[x])
{
vis[x] = vis[x ^ 1] = true;
dfs(e.to, x);
}
}
if (ind >= 0)
sk.push(ind);
}
//返回1:欧拉回路 返回2:欧拉路经
int solve(int s)
{
ans.clear();
int cnt = 0;
REP(i, n)
{
if (deg[i] == 1)
{
if (++cnt > 2) return 0;
s = i;
}
else if (deg[i] % 2 != 0) return 0;
}
while (!sk.empty()) sk.pop();
REP(i, m) vis[i] = false;
dfs(s, -1);
REP(i, m) if (!vis[i]) return 0;
while (!sk.empty())
{
ans.push_back(sk.top());
sk.pop();
}
return cnt != 0 ? 1 : 2;
}
} graph;
int a[MAXN], b[MAXN];
int main()
{
int T, n;
RI(T);
FE(kase, 1, T)
{
graph.init(60);
RI(n);
REP(i, n)
{
RII(a[i], b[i]);
graph.addEdge(a[i], b[i]);
}
printf("Case #%d\n", kase);
if (graph.solve(a[0]) == 2)
{
vector<int>& ans = graph.ans;
REP(i, ans.size())
{
Edge& e = graph.edges[ans[i]];
int u = e.from, v = e.to;
printf("%d %d\n", u, v);
}
}
else puts("some beads may be lost");
puts("");
}
return 0;
}