QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#106647 | #2341. Dead-End Detector | idontreallyknow | AC ✓ | 620ms | 79432kb | C++17 | 3.9kb | 2023-05-18 14:57:23 | 2023-05-18 14:57:24 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using pii = pair<int,int>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T uid(T x, T y) {return uniform_int_distribution<T>(x,y)(rng);}
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {os << "["; for (int i = 0; i < v.size(); i++) {if (i) os << ", "; os << v[i];} return os << "]";};
template<typename... Ts> ostream& operator<<(ostream& os, const pair<Ts...>& x) {return os << "(" <<x.first << ", " << x.second << ")";};
template<typename T> void pv(T a, T b, string s = "") {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "] " << s << "\n";}
template<typename T> void pv(const T& t, string s = "") {pv(begin(t), end(t), s);}
template<typename... T> void dbg(T... t) {initializer_list<int>{(cerr << t << " ", 0)...}; cerr << "\n";}
template<typename T, typename U> void chmin(T& t, U u) {if (t > u) t = u;}
template<typename T, typename U> void chmax(T& t, U u) {if (t < u) t = u;}
#define bug(x...) cerr << "[" << #x << "] = ", dbg(x)
#define fi first
#define se second
#define SZ(x) ((int)((x).size()))
const int mx = 5e5+5;
vector<int> adj[mx], cyc;
vector<int> allvis;
vector<pii> ans;
int vis[mx];
bool tr = true;
void dfs(int v, int p) {
vis[v] = 1;
allvis.push_back(v);
for (int a : adj[v]) {
if (a == p) continue;
if (vis[a]) {
tr = false;
if (SZ(cyc) == 0) {
cyc.push_back(v);
}
}
else {
dfs(a,v);
}
}
}
void dfs1(int v, int p) {
int ct = 0;
for (int a : adj[v]) {
if (a == p) continue;
dfs1(a,v);
ct++;
}
if (ct == 0 && p != 0) ans.emplace_back(v,p);
}
int dep[mx], oncyc[mx], vis1[mx];
void dfs2(int v, int p) {
vis1[v] = 1;
for (int a : adj[v]) {
if (a == p) continue;
if (vis1[a]) {
oncyc[v] = 1;
continue;
}
dep[a] = dep[v]+1;
dfs2(a,v);
oncyc[v] |= oncyc[a];
}
}
void dfs3(int v, int p) {
for (int a : adj[v]) {
if (dep[a] == dep[v]+1) {
dfs3(a,v);
}
}
if (p != 0 && !oncyc[v] && oncyc[p]) {
ans.emplace_back(p,v);
}
}
void solve(istream& cin = std::cin, ostream& cout = std::cout) {
int n,m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a,b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
tr = true;
cyc.clear();
allvis.clear();
// check if tree
dfs(i,0);
if (tr) {
// if it is, just mark the leaves
if (SZ(allvis) == 2) {
ans.emplace_back(allvis[0],allvis[1]);
ans.emplace_back(allvis[1],allvis[0]);
continue;
}
int rt = i;
for (int v : allvis) {
if (SZ(adj[v]) > 1) rt = v;
}
dfs1(rt,0);
} else {
assert(SZ(cyc) == 1);
int rt = cyc[0];
dfs2(rt,0);
dfs3(rt,0);
}
}
}
sort(ans.begin(),ans.end());
cout << SZ(ans) << '\n';
for (auto [a,b] : ans) cout << a << ' ' << b << '\n';
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
// cin >> t;
for (int q = 1; q <= t; q++) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 17792kb
Test #2:
score: 0
Accepted
time: 1ms
memory: 18400kb
Test #3:
score: 0
Accepted
time: 431ms
memory: 79308kb
Test #4:
score: 0
Accepted
time: 7ms
memory: 17224kb
Test #5:
score: 0
Accepted
time: 5ms
memory: 15588kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 17260kb
Test #7:
score: 0
Accepted
time: 1ms
memory: 17156kb
Test #8:
score: 0
Accepted
time: 5ms
memory: 17032kb
Test #9:
score: 0
Accepted
time: 1ms
memory: 17196kb
Test #10:
score: 0
Accepted
time: 4ms
memory: 17248kb
Test #11:
score: 0
Accepted
time: 7ms
memory: 16808kb
Test #12:
score: 0
Accepted
time: 1ms
memory: 16756kb
Test #13:
score: 0
Accepted
time: 89ms
memory: 22748kb
Test #14:
score: 0
Accepted
time: 57ms
memory: 23508kb
Test #15:
score: 0
Accepted
time: 439ms
memory: 37364kb
Test #16:
score: 0
Accepted
time: 349ms
memory: 40720kb
Test #17:
score: 0
Accepted
time: 351ms
memory: 44604kb
Test #18:
score: 0
Accepted
time: 386ms
memory: 45756kb
Test #19:
score: 0
Accepted
time: 2ms
memory: 19248kb
Test #20:
score: 0
Accepted
time: 4ms
memory: 17772kb
Test #21:
score: 0
Accepted
time: 0ms
memory: 18076kb
Test #22:
score: 0
Accepted
time: 103ms
memory: 25644kb
Test #23:
score: 0
Accepted
time: 597ms
memory: 63840kb
Test #24:
score: 0
Accepted
time: 139ms
memory: 73488kb
Test #25:
score: 0
Accepted
time: 482ms
memory: 71664kb
Test #26:
score: 0
Accepted
time: 139ms
memory: 79428kb
Test #27:
score: 0
Accepted
time: 606ms
memory: 79432kb
Test #28:
score: 0
Accepted
time: 592ms
memory: 79396kb
Test #29:
score: 0
Accepted
time: 594ms
memory: 79400kb
Test #30:
score: 0
Accepted
time: 124ms
memory: 38012kb
Test #31:
score: 0
Accepted
time: 255ms
memory: 37888kb
Test #32:
score: 0
Accepted
time: 135ms
memory: 40924kb
Test #33:
score: 0
Accepted
time: 299ms
memory: 42404kb
Test #34:
score: 0
Accepted
time: 620ms
memory: 73412kb
Test #35:
score: 0
Accepted
time: 402ms
memory: 37820kb
Test #36:
score: 0
Accepted
time: 422ms
memory: 43728kb
Test #37:
score: 0
Accepted
time: 442ms
memory: 51192kb
Test #38:
score: 0
Accepted
time: 383ms
memory: 36004kb
Test #39:
score: 0
Accepted
time: 297ms
memory: 34628kb
Test #40:
score: 0
Accepted
time: 462ms
memory: 50496kb
Test #41:
score: 0
Accepted
time: 420ms
memory: 41068kb
Test #42:
score: 0
Accepted
time: 346ms
memory: 37716kb
Test #43:
score: 0
Accepted
time: 450ms
memory: 38352kb
Test #44:
score: 0
Accepted
time: 376ms
memory: 44908kb
Test #45:
score: 0
Accepted
time: 323ms
memory: 46548kb