QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#177955 | #2341. Dead-End Detector | PetroTarnavskyi# | AC ✓ | 1003ms | 116376kb | C++17 | 1.8kb | 2023-09-13 16:26:33 | 2023-09-13 16:26:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b) - 1; i >= (a); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int MAX = 500'447;
set<int> g[MAX];
bool used[MAX];
bool del[MAX];
VI vert;
void dfs(int v)
{
used[v] = 1;
vert.PB(v);
for (auto to : g[v])
if (!used[to])
dfs(to);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(15);
int n, m;
cin >> n >> m;
FOR (i, 0, m)
{
int a, b;
cin >> a >> b;
a--, b--;
g[a].insert(b);
g[b].insert(a);
}
vector<PII> ans;
FOR (i, 0, n)
{
if (!used[i])
{
dfs(i);
int cnt = 0;
for (auto v : vert)
cnt += SZ(g[v]);
assert(cnt % 2 == 0);
cnt /= 2;
if (cnt < SZ(vert))
{
for (auto v : vert)
{
if (SZ(g[v]) == 1)
ans.PB({v, *g[v].begin()});
}
}
else
{
vector<PII> a;
set<PII> s;
for (auto v : vert)
{
s.insert({SZ(g[v]), v});
}
while (s.begin()->F == 1)
{
int v = s.begin()->S;
s.erase(s.begin());
int u = *g[v].begin();
a.PB({v, u});
del[v] = 1;
g[v].erase(u);
s.erase({SZ(g[u]), u});
g[u].erase(v);
s.insert({SZ(g[u]), u});
}
for (auto [v, u] : a)
{
assert(del[v]);
if (!del[u])
ans.PB({u, v});
}
}
vert.clear();
}
}
sort(ALL(ans));
cout << SZ(ans) << '\n';
for (auto [v, u] : ans)
cout << v + 1 << ' ' << u + 1 << '\n';
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 27996kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 27348kb
Test #3:
score: 0
Accepted
time: 370ms
memory: 112448kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 28316kb
Test #5:
score: 0
Accepted
time: 7ms
memory: 27740kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 27060kb
Test #7:
score: 0
Accepted
time: 2ms
memory: 27348kb
Test #8:
score: 0
Accepted
time: 6ms
memory: 27764kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 27088kb
Test #10:
score: 0
Accepted
time: 0ms
memory: 27160kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 27556kb
Test #12:
score: 0
Accepted
time: 7ms
memory: 28284kb
Test #13:
score: 0
Accepted
time: 197ms
memory: 74372kb
Test #14:
score: 0
Accepted
time: 476ms
memory: 73980kb
Test #15:
score: 0
Accepted
time: 297ms
memory: 79096kb
Test #16:
score: 0
Accepted
time: 452ms
memory: 81608kb
Test #17:
score: 0
Accepted
time: 726ms
memory: 106444kb
Test #18:
score: 0
Accepted
time: 719ms
memory: 105320kb
Test #19:
score: 0
Accepted
time: 3ms
memory: 28020kb
Test #20:
score: 0
Accepted
time: 3ms
memory: 27496kb
Test #21:
score: 0
Accepted
time: 3ms
memory: 27480kb
Test #22:
score: 0
Accepted
time: 90ms
memory: 47876kb
Test #23:
score: 0
Accepted
time: 511ms
memory: 107220kb
Test #24:
score: 0
Accepted
time: 87ms
memory: 89056kb
Test #25:
score: 0
Accepted
time: 295ms
memory: 89000kb
Test #26:
score: 0
Accepted
time: 179ms
memory: 112572kb
Test #27:
score: 0
Accepted
time: 501ms
memory: 112464kb
Test #28:
score: 0
Accepted
time: 503ms
memory: 112416kb
Test #29:
score: 0
Accepted
time: 515ms
memory: 112580kb
Test #30:
score: 0
Accepted
time: 88ms
memory: 55108kb
Test #31:
score: 0
Accepted
time: 159ms
memory: 55240kb
Test #32:
score: 0
Accepted
time: 251ms
memory: 82368kb
Test #33:
score: 0
Accepted
time: 412ms
memory: 80368kb
Test #34:
score: 0
Accepted
time: 798ms
memory: 116376kb
Test #35:
score: 0
Accepted
time: 290ms
memory: 79272kb
Test #36:
score: 0
Accepted
time: 308ms
memory: 81052kb
Test #37:
score: 0
Accepted
time: 308ms
memory: 83708kb
Test #38:
score: 0
Accepted
time: 298ms
memory: 75980kb
Test #39:
score: 0
Accepted
time: 252ms
memory: 70324kb
Test #40:
score: 0
Accepted
time: 685ms
memory: 101164kb
Test #41:
score: 0
Accepted
time: 1003ms
memory: 104556kb
Test #42:
score: 0
Accepted
time: 373ms
memory: 75208kb
Test #43:
score: 0
Accepted
time: 576ms
memory: 76908kb
Test #44:
score: 0
Accepted
time: 719ms
memory: 106296kb
Test #45:
score: 0
Accepted
time: 723ms
memory: 106580kb