/* In the name of God */
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <list>
#include <map>
#include <numeric>
#include <limits>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef map<int, int> MPII;
typedef vector<int> VI;
typedef vector<ll> VL;
#define PB push_back
#define POP pop_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort(all(a))
#define REVERSE(a) reverse(all(a))
#define sep ' '
#define endl '\n'
#define dbg(x) cerr << '[' << #x << ": " << x << "]\n"
#define dbg2(x, y) cerr << '[' << #x << ": " << x << ", " << #y << ": " << y << "]\n"
#define Yes cout << "Yes\n"
#define YES cout << "YES\n"
#define No cout << "No\n"
#define NO cout << "NO\n"
#define OK cout << "OK\n"
const ll INF = (ll)8e18 + 1386;
const ld eps = 0.000000000000001;
const int MOD = 1e9 + 7;
inline int mod_add(int a, int b){ int res = a + b; return (res >= MOD? res - MOD : res); }
inline int mod_neg(int a, int b){ int res = (abs(a - b) < MOD? a - b : (a - b) % MOD); return (res < 0? res + MOD : res); }
inline int mod_mlt(int a, int b){ return (1ll * a * b % MOD); }
inline string intToString(ll a){ char x[100]; sprintf(x, "%lld", a); string s = x; return s; }
inline ll stringToInt(string s){ ll res; char x[100]; strcpy(x, s.c_str()); sscanf(x, "%lld", &res); return res; }
inline void fileIO(string i, string o){ freopen(i.c_str(), "r", stdin); freopen(o.c_str(), "w", stdout); }
const int MAXN = 1e5 + 5;
int n, m, par[MAXN], par2[MAXN], ps[MAXN], parr[MAXN], root, h[MAXN];
VI N[MAXN];
bitset<MAXN> mark;
int getpar(int v, bool which = 1){
return ((which? par : par2)[v] == v ? v : (which? par : par2)[v] = getpar((which? par : par2)[v], which));
}
void dfs(int v, int height = 0){
mark[v] = 1;
h[v] = height++;
for (int u : N[v]){
if (!mark[u]){
parr[u] = v;
dfs(u, height);
}
else if (h[v] - h[u] > 1){
ps[v]++, ps[u]--;
}
}
}
void dfs_ans(int v){
mark[v] = 1;
for (int u : N[v]){
if (!mark[u]){
dfs_ans(u);
ps[v] += ps[u];
}
}
if (v == root) return;
if (ps[v] == 0) cout << v << sep << parr[v] << endl;
}
void merge(int u, int v){
int p1 = getpar(u), p2 = getpar(v);
if (p1 == p2){
p1 = getpar(u, 0), p2 = getpar(v, 0);
if (p1 != p2){
N[u].PB(v), N[v].PB(u);
par2[p1] = p2;
}
} else {
N[u].PB(v), N[v].PB(u);
par[p1] = p2;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
iota(par, par + MAXN, 0);
iota(par2, par2 + MAXN, 0);
cin >> n >> m;
for (int i = 0; i < m; i++){
int x, y;
cin >> x >> y;
merge(x, y);
}
for (int i = 1; i <= n; i++){
if (!mark[i]){
root = i;
parr[root] = root;
dfs(root);
}
}
mark.reset();
for (int i = 1; i <= n; i++){
if (!mark[i]){
root = i;
dfs_ans(root);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Incorrect |
2 ms |
3412 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3924 KB |
Output is correct |
2 |
Incorrect |
6 ms |
3668 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
3724 KB |
Output is correct |
2 |
Incorrect |
85 ms |
3584 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
141 ms |
4356 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
5820 KB |
Output is correct |
2 |
Correct |
204 ms |
5420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
10408 KB |
Output is correct |
2 |
Incorrect |
351 ms |
7544 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
523 ms |
11436 KB |
Output is correct |
2 |
Incorrect |
490 ms |
8944 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
712 ms |
13316 KB |
Output is correct |
2 |
Incorrect |
661 ms |
9268 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
859 ms |
13344 KB |
Output is correct |
2 |
Incorrect |
787 ms |
9312 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1043 ms |
12876 KB |
Output is correct |
2 |
Correct |
920 ms |
10528 KB |
Output is correct |