#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 2e5+5;
bool del[N];
set<pll> g[N];
vector<pll> ans;
priority_queue<pll, vector<pll>, greater<pll> > pq;
inline void no() { cout << "-1\n"; exit(0); }
int main(void)
{ IO
int n, i, m;
cin >> n >> m;
if (m != n-1) assert(0);
for (i=0; i < m; ++i)
{
int x, y; cin >> x >> y;
g[x].insert({y, 1}), g[y].insert({x, 0});
}
for (i=1; i <= n; ++i) pq.push({SZ(g[i]), i});
while (!pq.empty())
{
int deg = pq.top().F, x = pq.top().S; pq.pop();
if (del[x]) continue;
del[x] = 1;
pll p = *g[x].begin();
int v = p.F, f = p.S;
ans.pb(x, f);
g[v].erase({x, f^1});
if (!g[v].empty()) pq.push({SZ(g[v]), v});
}
for (i=1; i <= n; ++i) if (!del[i]) ans.pb(i, 0);
cout << SZ(ans) << '\n';
for (auto p : ans) cout << p.F << ' ' << p.S << '\n';
return 0;
}
Compilation message
naboj.cpp: In function 'int main()':
naboj.cpp:39:13: warning: unused variable 'deg' [-Wunused-variable]
39 | int deg = pq.top().F, x = pq.top().S; pq.pop();
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
10 ms |
19548 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
273 ms |
46396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
10 ms |
19548 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |