結果
| 問題 |
No.1553 Lovely City
|
| コンテスト | |
| ユーザー |
hongrock
|
| 提出日時 | 2021-06-18 22:08:01 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,391 bytes |
| コンパイル時間 | 2,425 ms |
| コンパイル使用メモリ | 205,484 KB |
| 最終ジャッジ日時 | 2025-01-22 09:19:51 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 4 WA * 22 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:77:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
77 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:80:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
80 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for(int i=(a); i<(n); ++i)
#define per(i, a, n) for(int i=(a); i>(n); --i)
#define pb emplace_back
#define mp make_pair
#define clr(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define lson o<<1
#define rson o<<1|1
#define gmid l[o]+r[o]>>1
using LL = long long;
using ULL = unsigned long long;
using pii = pair<int,int>;
using PLL = pair<LL, LL>;
using UI = unsigned int;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int N = 2e5 + 10;
vector<int> V[N], G[N], A[N];
vector<pii> ans;
int n, m, pre[N], deg[N], low[N], idx[N], scc, dfs_cnt, val[N], idx2[N];
stack<int> st;
int com;
void bfs(int x){
queue<int> Q;
Q.emplace(x);
idx2[x] = ++com;
while(!Q.empty()){
x = Q.front(); Q.pop();
for(int j : G[x]){
if(!idx2[j]){
idx2[j] = com;
Q.emplace(j);
}
}
}
}
void dfs(int u){
pre[u] = low[u] = ++dfs_cnt;
st.push(u);
for(int v : V[u]){
if(!pre[v]){
dfs(v);
low[u] = min(low[u], low[v]);
} else if(!idx[v]){
low[u] = min(low[u], pre[v]);
}
}
if(low[u] == pre[u]){
scc++;
for(;;){
int x = st.top(); st.pop();
idx[x] = scc;
G[scc].pb(x);
if(x == u) break;
}
}
}
int main(){
scanf("%d %d", &n, &m);
int x, y;
while(m--){
scanf("%d %d", &x, &y);
V[x].pb(y);
G[x].pb(y);
G[y].pb(x);
}
clr(idx2, 0);
com = 0;
rep(i, 1, n + 1){
if(!idx2[i]){
bfs(i);
}
}
rep(i, 1, n + 1) G[i].clear();
clr(pre, 0);
clr(idx, 0);
dfs_cnt = scc = 0;
scc = 0;
rep(i, 1, n + 1){
if(!pre[i]){
dfs(i);
}
}
rep(i, 1, scc+1){
val[i] = G[i][0];
for(int j=1; j<G[i].size(); ++j){
ans.pb(G[i][j-1], G[i][j]);
}
deg[i] = 0;
G[i].clear();
}
rep(i, 1, n + 1){
x = idx[i];
for(int j : V[i]){
y = idx[j];
if(x == y) continue;
G[x].pb(y);
++deg[y];
}
}
queue<int> Q;
rep(i, 1, scc + 1){
if(deg[i] == 0){
Q.emplace(i);
}
}
while(!Q.empty()){
x = Q.front(); Q.pop();
A[idx2[val[x]]].pb(val[x]);
for(int j : G[x]){
if(--deg[j] == 0){
Q.emplace(j);
}
}
}
rep(i, 1, com+1){
rep(j, 1, A[i].size()){
ans.pb(A[i][j-1], A[i][j]);
}
}
printf("%d\n", (int)ans.size());
for(pii p : ans) printf("%d %d\n", p.fi, p.se);
return 0;
}
hongrock