1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
| #include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 10;
int n, m;
bool st[N];
int a, b, c;
int h[N], e[N], ne[N], idx;
int q[N], fa[N], hh, tt;
void add(int u, int v) {
e[idx] = v, ne[idx] = h[u], h[u] = idx ++ ;
}
bool find(int u, int v) {
for (int i = h[u]; i != -1; i = ne[i]) {
if (e[i] == v) return true;
}
return false;
}
void swap(int &x, int &y) {
int z = x;
x = y;
y = z;
}
int main() {
memset(h, -1, sizeof h);
freopen("triangle.in", "r", stdin);
freopen("triangle.out", "w", stdout);
cin >> n >> m;
for (int i = 0; i < m; i ++ ) {
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
q[++ tt] = 1;
fa[tt] = -1;
st[1] = 1;
while (hh != tt) {
hh ++ ;
bool flag = false;
for (int i = h[q[hh]]; i != -1; i = ne[i]) {
int v = e[i];
if (fa[hh] != -1 && st[v] && v != fa[hh] && find(fa[hh], v)) {
a = fa[hh];
b = q[hh];
c = v;
if (a > b) swap(a, b);
if (b > c) swap(b, c);
if (a > b) swap(a, b);
flag = true;
break;
}
if (!st[v]) {
st[v] = true;
q[++ tt] = v;
fa[tt] = q[hh];
}
}
if (flag) break;
}
printf("%d %d %d \n", a, b, c);
return 0;
}
|