# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
527190 |
2022-02-17T03:27:03 Z |
maomao90 |
Pipes (CEOI15_pipes) |
C++17 |
|
2739 ms |
16600 KB |
#include <bits/stdc++.h>
using namespace std;
#define REP(i, j, k) for (int i = j; i < k; i++)
#define RREP(i, j, k) for (int i = j; i >= k; i--)
template<class T>
inline bool mxto(T &a, T b) {return a < b ? a = b, 1 : 0;}
template<class T>
inline bool mnto(T &a, T b) {return a > b ? a = b, 1 : 0;}
typedef long long ll;
#define FI first
#define SE second
#define MP make_pair
typedef pair<int, int> ii;
#define pb push_back
#define ALL(x) x.begin(), x.end()
typedef vector<int> vi;
typedef vector<ii> vii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
#define INF 1000000005
#define LINF 1000000000000000005ll
#define MAXN 100005
int n, m;
vi adj[MAXN];
int uf[MAXN], rnk[MAXN];
void init() {
REP (i, 0, MAXN) {
uf[i] = i;
rnk[i] = 0;
}
}
int findp(int u) {
if (uf[u] == u) return u;
return uf[u] = findp(uf[u]);
}
bool join(int a, int b) {
int pa = findp(a), pb = findp(b);
if (pa == pb) return 0;
if (rnk[pa] < rnk[pb]) swap(pa, pb);
if (rnk[pa] == rnk[pb]) rnk[pa]++;
uf[pb] = pa;
return 1;
}
int vis[MAXN], p[MAXN], lvl[MAXN];
int dfs(int u) {
int st = 1, mx = -1, mxid = -1;
REP (i, 0, adj[u].size()) {
int v = adj[u][i];
if (vis[v]) continue;
lvl[v] = lvl[u] + 1;
p[v] = u;
vis[v] = 1;
int tmp = dfs(v);
if (mxto(mx, tmp)) {
mxid = i;
}
st += tmp;
}
if (mxid != -1) {
swap(adj[u][0], adj[u][mxid]);
}
return st;
}
int pre[MAXN], head[MAXN], ptr;
int mppre[MAXN];
void hld(int u, int ch) {
//cerr << u << '\n';
head[u] = ch;
pre[u] = ptr++;
mppre[pre[u]] = u;
if (adj[u].empty()) return;
if (adj[u][0] != p[u]) {
hld(adj[u][0], ch);
}
REP (i, 1, adj[u].size()) {
int v = adj[u][i];
if (p[u] == v) continue;
hld(v, v);
}
}
int psm[MAXN];
void incre(int l, int r) {
assert(l <= r);
psm[l]++;
psm[r + 1]--;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> m;
init();
REP (i, 0, m) {
int u, v; cin >> u >> v;
if (join(u, v)) {
cerr << u << " --- " << v << '\n';
adj[u].pb(v);
adj[v].pb(u);
}
}
REP (i, 1, n + 1) {
if (vis[i]) continue;
vis[i] = 1;
p[i] = -1;
dfs(i);
hld(i, i);
}
init();
cin.seekg(0, cin.beg);
cin >> n >> m;
// head has smallest pre
REP (i, 0, m) {
int a, b; cin >> a >> b;
cerr << ' ' << a << ' ' << b << '\n';
if (join(a, b)) {
continue;
}
while (head[a] != head[b]) {
if (lvl[head[a]] < lvl[head[b]]) swap(a, b);
incre(pre[head[a]], pre[a]);
a = p[head[a]];
}
if (a != b) {
if (lvl[a] < lvl[b]) swap(a, b);
incre(pre[b] + 1, pre[a]);
}
}
REP (i, 0, n) {
if (i) psm[i] += psm[i - 1];
if (p[mppre[i]] == -1 || psm[i] > 0) continue;
cout << mppre[i] << ' ' << p[mppre[i]] << '\n';
}
return 0;
}
/*
10 11
1 7
1 8
1 6
2 8
6 7
5 8
2 5
2 3
2 4
3 4
10 9
*/
Compilation message
pipes.cpp: In function 'int dfs(int)':
pipes.cpp:4:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define REP(i, j, k) for (int i = j; i < k; i++)
......
56 | REP (i, 0, adj[u].size()) {
| ~~~~~~~~~~~~~~~~~~~
pipes.cpp:56:2: note: in expansion of macro 'REP'
56 | REP (i, 0, adj[u].size()) {
| ^~~
pipes.cpp: In function 'void hld(int, int)':
pipes.cpp:4:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define REP(i, j, k) for (int i = j; i < k; i++)
......
85 | REP (i, 1, adj[u].size()) {
| ~~~~~~~~~~~~~~~~~~~
pipes.cpp:85:2: note: in expansion of macro 'REP'
85 | REP (i, 1, adj[u].size()) {
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3404 KB |
Output is correct |
2 |
Correct |
2 ms |
3404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3660 KB |
Output is correct |
2 |
Correct |
11 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
3540 KB |
Output is correct |
2 |
Correct |
194 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
339 ms |
3992 KB |
Output is correct |
2 |
Correct |
397 ms |
3984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
615 ms |
4820 KB |
Output is correct |
2 |
Correct |
532 ms |
11888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
832 ms |
7556 KB |
Output is correct |
2 |
Correct |
724 ms |
7788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1384 ms |
8144 KB |
Output is correct |
2 |
Correct |
1296 ms |
8580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1781 ms |
9264 KB |
Output is correct |
2 |
Correct |
1668 ms |
9816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2251 ms |
16600 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2739 ms |
10044 KB |
Output is correct |
2 |
Correct |
2632 ms |
14832 KB |
Output is correct |