Skip to content

Commit f32dc40

Browse files
committed
✨ P3465
1 parent b85d788 commit f32dc40

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

Problem/Luogu/P3465.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <cstdio>
2+
#include <tuple>
3+
#include <vector>
4+
constexpr int MaxN = 1e5 + 5;
5+
int n, m;
6+
int answer[MaxN];
7+
bool vis[MaxN];
8+
std::vector<int> g[MaxN];
9+
void dfsSet(int u, int f)
10+
{
11+
if (vis[u])
12+
{
13+
return;
14+
}
15+
vis[u] = true;
16+
answer[u] = f;
17+
for (const auto &v : g[u])
18+
{
19+
if (v == f)
20+
{
21+
continue;
22+
}
23+
dfsSet(v, u);
24+
}
25+
}
26+
std::tuple<bool, int, bool> dfs(int u, int f)
27+
{
28+
if (vis[u])
29+
{
30+
answer[u] = f;
31+
return {true, u, false};
32+
}
33+
vis[u] = true;
34+
bool suc = false;
35+
for (const auto &v : g[u])
36+
{
37+
if (v == f)
38+
{
39+
continue;
40+
}
41+
auto [found, c, pass] = dfs(v, u);
42+
if (found)
43+
{
44+
if (c == u)
45+
{
46+
pass = true;
47+
}
48+
if (f == 0)
49+
{
50+
suc = true;
51+
break;
52+
}
53+
if (pass)
54+
{
55+
answer[f] = u;
56+
}
57+
if (!pass)
58+
{
59+
answer[u] = f;
60+
}
61+
for (const auto &v : g[u])
62+
{
63+
if (v == f)
64+
{
65+
continue;
66+
}
67+
dfsSet(v, u);
68+
}
69+
return {found, c, pass};
70+
}
71+
}
72+
vis[u] = false;
73+
if (f == 0 && suc)
74+
{
75+
vis[u] = true;
76+
for (const auto &v : g[u])
77+
{
78+
if (v == f)
79+
{
80+
continue;
81+
}
82+
dfsSet(v, u);
83+
}
84+
return {true, 0, false};
85+
}
86+
return {false, 0, false};
87+
}
88+
int main()
89+
{
90+
scanf("%d%d", &n, &m);
91+
for (int i = 1; i <= m; i++)
92+
{
93+
int u, v;
94+
scanf("%d%d", &u, &v);
95+
g[u].push_back(v);
96+
g[v].push_back(u);
97+
}
98+
for (int i = 1; i <= n; i++)
99+
{
100+
if (!vis[i])
101+
{
102+
const auto &[res, _a, _b] = dfs(i, 0);
103+
if (!res)
104+
{
105+
printf("NIE\n");
106+
return 0;
107+
}
108+
}
109+
}
110+
printf("TAK\n");
111+
for (int i = 1; i <= n; i++)
112+
{
113+
printf("%d\n", answer[i]);
114+
}
115+
return 0;
116+
}

0 commit comments

Comments
 (0)