結果
| 問題 |
No.1605 Matrix Shape
|
| コンテスト | |
| ユーザー |
iomir
|
| 提出日時 | 2023-04-16 18:57:26 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,506 bytes |
| コンパイル時間 | 2,025 ms |
| コンパイル使用メモリ | 186,708 KB |
| 実行使用メモリ | 39,120 KB |
| 最終ジャッジ日時 | 2024-10-12 01:45:41 |
| 合計ジャッジ時間 | 6,116 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 11 WA * 21 RE * 2 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
const ll INF=1ll<<60;
using vp=vector<pair<ll, ll>>;
using P = pair<ll,ll>;
struct unionfind{
ll N;
vector<ll> par,Rank,Size;
unionfind(ll n):N(n){
par=vector<ll>(N),Rank=vector<ll>(N,1),Size=vector<ll>(N,1);
for(int i=0;i<N;i++) par[i]=i;
};
ll find(ll x){
if(par[x]==x) return x;
else return par[x]=find(par[x]);
}
bool same(ll a,ll b){
return find(a)==find(b);
}
void unite(ll a,ll b){
if(same(a,b))return;
a=find(a);
b=find(b);
ll s=Size[a]+Size[b];
if(Rank[b]>Rank[a]) par[a]=b;
else par[b]=a;
if(Rank[a]==Rank[b]) Rank[a]++;
Size[find(a)]=s;
}
ll size(ll a){
return Size[find(a)];
}
};
int main(){
ll N;
cin>>N;
unionfind uf(2e5);
vll A(2e5);
vvll ab(2e5);
vp hw(N);
vll deg(2e5);
set<P> st;
for(int i=0;i<N;i++){
cin>>hw[i].first>>hw[i].second;
hw[i].first--;hw[i].second--;
if(hw[i].first!=hw[i].second){
A[hw[i].second]++;
deg[hw[i].first]++;
deg[hw[i].second]++;
ab[hw[i].first].push_back(hw[i].second);
}else st.insert(make_pair(hw[i].first,hw[i].second));
uf.unite(hw[i].first,hw[i].second);
}
ll s=uf.find(hw[0].first);
for(int i=0;i<N;i++){
if(uf.find(hw[i].first)!=s){
cout << 0 << endl;
return 0;
}
}
queue<ll> q;
for(int i=0;i<2e5;i++){
if(A[i]==0&&ab[i].size())q.push(i);
}
ll cnt=0;
bool a=true;
while(!q.empty()){
auto x=q.front();q.pop();
if(q.size()!=0)a=false;
cnt++;
for(auto to:ab[x]){
A[to]--;
if(A[to]==0)q.push(to);
}
}
if(cnt==N){
if(a)cout << 1 << endl;
else cout << 0 <<endl;
return 0;
}
for(int i=0;i<2e5;i++){
if(ab[i].size()){
if(deg[i]!=2){
cout<<0<<endl;
return 0;
}
}
}
vll n;
ll now=hw[0].first;
for(int i=0;i<N+1;i++){
n.push_back(now);
now=ab[now][0];
}
for(int i=0;i<N;i++){
st.insert(make_pair(n[i+1],n[i]));
cout<<n[i]<<endl;
}
cout <<st.size()<< endl;
}
iomir