#include <bits/stdc++.h>
// BAI NHU LON
// #pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 3e5+10;
const int MAXA = 2e3+10;
const int INF = 1e18+10;
const int LOG = 19;
const int MOD = 1e9+7;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
int n, m, q;
int u[MAXN], v[MAXN];
vector <pii> adj[MAXN];
int tim, disc[MAXN], low[MAXN], col[MAXN];
int up[MAXN], dw[MAXN], br[MAXN];
int ty[MAXN];
void dfs(int nw, int idxpar){
disc[nw] = low[nw] = ++tim;
for(auto [nx, idx] : adj[nw]){
if(idx == idxpar) continue;
if(disc[nx] == -1){
dfs(nx, idx);
if(disc[nx] == low[nx]){
cout << idx << '\n';
br[idx] = 1; // bridge
}
low[nw] = min(low[nw], low[nx]);
} else low[nw] = min(low[nw], disc[nx]); // udh pernah vis
}
}
void color(int nw, int COL){
col[nw] = COL;
for(auto [nx, idx] : adj[nw]){
if(col[nx]!=-1 || br[idx]) continue;
color(nx, COL);
}
}
vector <pii> edge[MAXN];
int anc[MAXN][LOG+5], dep[MAXN];
void trav(int nw, int par){
disc[nw] = 1;
anc[nw][0] = par; dep[nw] = dep[par]+1;
for(auto [nx, idx] : edge[nw]){
if(nx==par) continue;
trav(nx, nw);
}
}
int LCA(int x, int y){
if(dep[x] > dep[y]) swap(x, y);
for(int i=LOG-1; i>=0; i--){
if(dep[anc[y][i]] >= dep[x]){
y = anc[y][i];
}
}
if(x==y) return x;
for(int i=LOG-1; i>=0; i--){
if(anc[y][i] != anc[x][i]){
y = anc[y][i]; x = anc[x][i];
}
}
return anc[x][0];
}
int idxedge[MAXN];
void bd(int nw){
// cout << nw << " nw\n";
disc[nw] = 1;
for(auto [nx, idx] : edge[nw]){ // nw->nx = idx
if(disc[nx]==1) continue;
bd(nx);
idxedge[nx] = idx;
up[nw] += up[nx]; dw[nw] += dw[nx];
}
cerr << up[nw] << ' ' << dw[nw] << '\n';
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
freopen("test.inp","r",stdin); freopen("test.ans","w",stdout); freopen("test.err","w",stderr);
cin >> n >> m;
for(int i=1; i<=m; i++){
cin >> u[i] >> v[i];
adj[u[i]].pb({v[i], i}); adj[v[i]].pb({u[i], i});
}
// 1. detect bridge
memset(disc, -1, sizeof disc);
for(int i=1; i<=n; i++){
if(disc[i] == -1) dfs(i, 0);
}
// 2. bridge tree
int cnt = 0; memset(col, -1, sizeof col);
for(int i=1; i<=n; i++){
if(col[i] == -1) color(i, ++cnt);
}
for(int i=1; i<=m; i++){
if(col[u[i]] == col[v[i]]) continue;
edge[col[u[i]]].pb({col[v[i]], i});
edge[col[v[i]]].pb({col[u[i]], -i});
cout << col[u[i]] << ' ' << col[v[i]] << '\n';
}
// for(int i=1; i<=n; i++){
// cout << i << ' ' << col[i] << " pp\n";
// }
// for(int i=1; i<=m; i++){
// if(br[i]) cout << u[i] << ' ' << v[i] << " bri\n";
// }
// LCA
memset(disc, -1, sizeof disc);
for(int i=1; i<=n; i++){
if(disc[i]==-1) trav(i, 0);
}
// cout << "pp\n";
for(int j=1; j<LOG; j++)
for(int i=1; i<=n; i++)
anc[i][j] = anc[ anc[i][j-1] ][j-1];
cin >> q;
while(q--){
int x, y; cin >> x >> y;
x = col[x]; y = col[y];
int lca = LCA(x, y);
up[x]++; up[lca]--;
dw[y]++; dw[lca]--;
cout << lca << " lca\n";
cout << x << ' ' << dep[x]-1 << " up\n";
cout << y << ' ' << dep[y]-1 << " dw\n";
}
// for(int i=1; i<=3; i++){
// cout << i << ' ' << up[i] << ' ' << dw[i] << "pre\n";
// }
memset(disc, -1, sizeof disc);
for(int i=1; i<=cnt; i++){
if(disc[i] == -1){
// cout << i << " ii\n";
bd(i);
}
}
for(int i=1; i<=n; i++){ // node di bridge tree
// cout << i << ' ' << idxedge[i] << " pp\n";
if(up[i]!=0 && dw[i]!=0) continue;
int id = idxedge[i];
cout << i << ' ' << up[i] << ' ' << dw[i] << " updw\n";
if(up[i]!=0){ // nx->nw
if(id < 0) ty[-id] = -1;
else ty[id] = 1;
}
if(dw[i]!=0){ // nw->nx
if(id < 0) ty[-id] = 1;
else ty[id] = -1;
}
}
// OUTPUT
for(int i=1; i<=m; i++){
if(ty[i]==1) cout << 'L';
else if(ty[i]==-1) cout << 'R';
else cout << 'B';
}
// cout << '\n';
}
Compilation message
oneway.cpp: In function 'int main()':
oneway.cpp:97:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("test.inp","r",stdin); freopen("test.ans","w",stdout); freopen("test.err","w",stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:97:43: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("test.inp","r",stdin); freopen("test.ans","w",stdout); freopen("test.err","w",stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:97:75: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("test.inp","r",stdin); freopen("test.ans","w",stdout); freopen("test.err","w",stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
26972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
26972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
26972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |