This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Problem: H. Hai đường đi
// Contest: Codeforces - 2023 winter contest #41, Dalat winter training camp, round 1
// URL: https://gspvh23.contest.codeforces.com/group/FIWwc7nF6c/contest/484130/problem/H
// Memory Limit: 512 MB
// Time Limit: 2250 ms
//
// Powered by CP Editor (https://cpeditor.org)
/*CP template version 3.0 - Laconic*/
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fs first
#define sc second
//use struct instead of pair<int, ii>
#define int long long
#define show(v) {for (auto i : v) cout << i << " "; cout << endl;}
#define showlr(v, l, r) {for (int i = l; i <= r; i++) cout << v[i] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define SUnique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define forlr(i, l, r) for (int i = l; i <= r; i++)
#define forrl(i, r, l) for (int i = r; i >= l; i--)
#pragma GCC Optimize("O2")
#define endl "\n"
#define precise(x) cout << fixed << setprecision(x);
using namespace std;
const int N = 5e5 + 100;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int LOG = 25;
const long long LINF = 1e15 + 100;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
ostream& operator << (ostream &os, ii a) {
os << a.fs << ' ' << a.sc;
return os;
}
int binPow(int a, int b, int m = LLONG_MAX) {
int prod = 1;
a %= m;
while (b) {
if (b & 1) prod = prod * a % m;
a = a * a % m;
b /= 2;
}
return prod;
}
void setIO(string s) {
if (s.empty()) return;
freopen((s + ".inp").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
//Try Brute Force (maybe there is a pattern)
//Try DP (Weird constraints (sum of all elements <= 10000), small constraints)
//Try Graph (a pair of number, condition between elements)
//Try Geometry (Same as graph, for coordinate)
//Try switching between problem
//Put down what you have on paper
//IMPORTANT: WRITE DOWN THE FORMULA
int n, m, q, k;
int arr[N];
vector<int> adj[N], adj1[N];
int low[N], num[N], head[N], timeDfs;
stack<int> st;
int bccId[N], bcc;
void tarjan(int u, int pre) {
st.push(u);
num[u] = low[u] = ++timeDfs;
for (int v : adj[u]) {
if (v == pre) continue;
if (num[v]) {
low[u] = min(low[u], num[v]);
} else {
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
}
if (num[u] == low[u]) {
bcc++;
int tmp;
head[bcc] = u;
do {
tmp = st.top();
bccId[tmp] = bcc;
st.pop();
num[u] = INF;
} while (tmp != u);
}
}
vector<int> leaf;
void dfs1(int u, int pre) {
if (adj1[u].size() == 1) {
leaf.push_back(u);
if (u != pre) return;
}
for (int v : adj1[u]) {
if (v == pre) continue;
dfs1(v, u);
}
}
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
ii edges[N];
void solve() {
cin >> n;
forlr(i, 1, n) adj[i].clear(), adj1[i].clear();
leaf.clear();
fill(bccId + 1, bccId + n + 1, 0);
fill(low + 1, low + n + 1, 0);
fill(num + 1, num + n + 1, 0);
fill(head + 1, head + n + 1, 0);
timeDfs = 0;
bcc = 0;
while (st.size()) st.pop();
forlr(i, 1, m) {
int u, v; cin >> u >> v;
edges[i] = {u, v};
adj[u].push_back(v);
adj[v].push_back(u);
}
tarjan(1, 1);
forlr(i, 1, m) {
auto [u, v] = edges[i];
int idU = bccId[u], idV = bccId[v];
if (idU == idV) continue;
adj1[idU].push_back(idV);
adj1[idV].push_back(idU);
}
forlr(i, 1, n) SUnique(adj1[i]);
dfs1(1, 1);
vector<ii> res;
int sz = leaf.size();
if (sz == 2) {
cout << 1 << endl;
cout << head[leaf[0]] << " " << head[leaf[1]] << endl;
return;
}
if (sz <= 1) {
cout << 0 << endl;
return;
}
forlr(i, 0, (sz - 1) / 2) res.push_back({head[leaf[i]], head[leaf[i + sz / 2]]});
cout << res.size() << endl;
assert(res.size() == (sz + 1) / 2);
for (ii i : res) cout << i << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// setIO("biconnected");
int T = 1;
bool multiTest = 0;
if (multiTest) cin >> T;
while (T--) {
solve();
}
}
Compilation message (stderr)
net.cpp:28: warning: ignoring '#pragma GCC Optimize' [-Wunknown-pragmas]
28 | #pragma GCC Optimize("O2")
|
net.cpp: In function 'void solve()':
net.cpp:183:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
183 | auto [u, v] = edges[i];
| ^
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from net.cpp:11:
net.cpp:216:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
216 | assert(res.size() == (sz + 1) / 2);
| ~~~~~~~~~~~^~~~~~~~~~~~~~~
net.cpp: In function 'void setIO(std::string)':
net.cpp:71:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | freopen((s + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |