#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define rep(i , j , k) for (int i = j; i < (int)k; i++)
#define pb push_back
#define mt make_tuple
#define all(x) x.begin(),x.end()
typedef long long ll;
typedef pair<int , int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef complex<ld> point;
typedef pair<ld , ld> pld;
typedef vector<int> vi;
inline void fileIO(string s) {
// freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
template<class T , class S>
inline bool smin(T &a, S b) { return (T)b < a ? a = b , 1 : 0; }
template<class T , class S>
inline bool smax(T &a, S b) { return a < (T)b ? a = b , 1 : 0; }
constexpr int N = 1e5 + 10;
constexpr int MOD = 1e6 + 7;
template<typename T>
inline T mod(T &v) { return v = (v % MOD + MOD) % MOD; }
template<typename S, typename T>
inline S add(S &l, T r) { return mod(l += r); }
int n, m;
vi adj[N];
int p[N];
int get_par(int v) {
if (p[v])
return p[v] = get_par(p[v]);
return v;
}
inline bool merge(int v, int u) {
v = get_par(v);
u = get_par(u);
if (v == u) return false;
p[v] = u;
return true;
}
bitset<N> mark;
int lvl[N], par[18][N], Tm , st[N], en[N];
void dfs(int v, int pa = 0) {
st[v] = Tm++;
lvl[v] = lvl[pa] + 1;
mark[v] = true;
par[0][v] = pa;
rep(i , 1 , 18)
par[i][v] = par[i - 1][par[i - 1][v]];
for (auto e : adj[v])
if (e ^ pa)
dfs(e , v);
en[v] = Tm;
}
inline int LCA(int v, int u) {
if (lvl[v] < lvl[u]) swap(v, u);
for (int i = 17; ~i; i--)
if (st[par[i][v]] > st[u] || en[par[i][v]] < en[u])
v = par[i][v];
return par[0][v];
}
int val[N];
void dfs2(int v , int pa = 0) {
mark[v] = true;
for (auto e : adj[v])
if (e ^ pa) {
dfs2(e , v);
val[v] += val[e];
}
if (val[v] == 1)
cout << v << ' ' << pa << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef LOCAL
//fileIO("superposition");
#endif
cin >> n >> m;
rep(i , 0 , m)
{
int u , v;
cin >> u >> v;
if (merge(u , v)) {
adj[u].pb(v);
adj[v].pb(u);
}
}
st[0] = -1;
rep(i , 1 , n + 1)
if (!mark[i])
dfs(i);
en[0] = Tm;
cin.seekg(0);
cin >> n >> m;
rep(i , 0 , m) {
int u , v;
cin >> u >> v;
int lca = LCA(u , v);
if (lca == u || lca == v) {
val[u ^ v ^ lca]++;
val[lca]--;
}
else {
val[u]++;
val[v]++;
val[lca] -= 2;
}
}
mark.reset();
rep(i , 1 , n + 1)
if (!mark[i])
dfs2(i);
return 0;
}
Compilation message
pipes.cpp: In function 'int LCA(int, int)':
pipes.cpp:78:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if (st[par[i][v]] > st[u] || en[par[i][v]] < en[u])
^~
pipes.cpp:80:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
return par[0][v];
^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
3300 KB |
Output is correct |
2 |
Correct |
14 ms |
3328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
3264 KB |
Output is correct |
2 |
Correct |
333 ms |
3256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
766 ms |
4112 KB |
Output is correct |
2 |
Correct |
918 ms |
3968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1334 ms |
5756 KB |
Output is correct |
2 |
Correct |
1087 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2771 ms |
11232 KB |
Output is correct |
2 |
Correct |
3375 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5021 ms |
12528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5052 ms |
14828 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5060 ms |
14824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5048 ms |
14200 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |