結果

問題 No.1254 補強への架け橋
ユーザー 👑 potato167potato167
提出日時 2022-02-26 16:44:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 2,761 bytes
コンパイル時間 2,419 ms
コンパイル使用メモリ 219,944 KB
実行使用メモリ 24,124 KB
最終ジャッジ日時 2023-09-17 18:37:21
合計ジャッジ時間 17,479 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 3 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 3 ms
4,380 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 1 ms
4,380 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 3 ms
4,380 KB
testcase_59 AC 2 ms
4,380 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 13 ms
5,464 KB
testcase_64 AC 4 ms
4,376 KB
testcase_65 AC 9 ms
4,840 KB
testcase_66 AC 7 ms
4,500 KB
testcase_67 AC 4 ms
4,376 KB
testcase_68 AC 8 ms
4,688 KB
testcase_69 AC 9 ms
4,768 KB
testcase_70 AC 4 ms
4,376 KB
testcase_71 AC 4 ms
4,384 KB
testcase_72 AC 9 ms
4,816 KB
testcase_73 AC 4 ms
4,376 KB
testcase_74 AC 9 ms
4,724 KB
testcase_75 AC 7 ms
4,468 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 6 ms
4,376 KB
testcase_78 AC 12 ms
5,056 KB
testcase_79 AC 12 ms
5,216 KB
testcase_80 AC 10 ms
4,824 KB
testcase_81 AC 12 ms
5,320 KB
testcase_82 AC 11 ms
5,260 KB
testcase_83 AC 228 ms
22,632 KB
testcase_84 AC 205 ms
22,188 KB
testcase_85 AC 109 ms
15,064 KB
testcase_86 AC 166 ms
19,508 KB
testcase_87 AC 192 ms
21,356 KB
testcase_88 AC 19 ms
6,112 KB
testcase_89 AC 205 ms
22,444 KB
testcase_90 AC 111 ms
15,456 KB
testcase_91 AC 86 ms
13,036 KB
testcase_92 AC 39 ms
8,428 KB
testcase_93 AC 155 ms
18,780 KB
testcase_94 AC 134 ms
17,696 KB
testcase_95 AC 137 ms
17,596 KB
testcase_96 AC 196 ms
21,544 KB
testcase_97 AC 68 ms
11,620 KB
testcase_98 AC 192 ms
21,500 KB
testcase_99 AC 101 ms
14,516 KB
testcase_100 AC 218 ms
22,972 KB
testcase_101 AC 32 ms
7,980 KB
testcase_102 AC 15 ms
5,728 KB
testcase_103 AC 37 ms
8,368 KB
testcase_104 AC 52 ms
10,344 KB
testcase_105 AC 154 ms
18,844 KB
testcase_106 AC 75 ms
12,392 KB
testcase_107 AC 208 ms
22,444 KB
testcase_108 AC 210 ms
22,236 KB
testcase_109 AC 154 ms
18,480 KB
testcase_110 AC 137 ms
17,140 KB
testcase_111 AC 148 ms
18,276 KB
testcase_112 AC 49 ms
9,936 KB
testcase_113 AC 128 ms
16,552 KB
testcase_114 AC 67 ms
12,136 KB
testcase_115 AC 21 ms
6,436 KB
testcase_116 AC 86 ms
13,544 KB
testcase_117 AC 47 ms
10,108 KB
testcase_118 AC 185 ms
21,644 KB
testcase_119 AC 97 ms
14,268 KB
testcase_120 AC 186 ms
20,796 KB
testcase_121 AC 40 ms
9,072 KB
testcase_122 AC 81 ms
13,384 KB
testcase_123 AC 1 ms
4,376 KB
testcase_124 AC 320 ms
24,012 KB
testcase_125 AC 296 ms
24,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"YES\n"; else cout<<"NO\n";}

namespace po167{
struct UFtree
{
	std::vector<int> wei;
	std::vector<int> q;
	int component;
	UFtree(int n):wei(n),component(n),par(n){
		for(int i=0;i<n;i++){
			wei[i]=1,par[i]=i;
		}
	}
	void intialize(){
		for(auto x:q){
			wei[root(x)]=1;
			par[x]=x;
		}
		component=(int)par.size();
		q={};
	}
	//根っこを返す
	int root(int a){
		if(a==par[a]) return a;
		return par[a]=root(par[a]);
	}
	//trueなら1,falseなら0
	int same(int a,int b){
		if(root(a)==root(b)) return 1;
		else return 0;
	}
	//a,bが違う根っこの元なら結合する,結合したらtrueを返す
	bool unite(int a,int b){
		a=root(a),b=root(b);
		if(a==b) return false;
		if(wei[a]<wei[b]) std::swap(a,b);
		par[b]=a;
		q.push_back(b);
		wei[a]+=wei[b];
		component--;
		return true;
	}
	private:
	std::vector<int> par;
};
}
using po167::UFtree;


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int N;
	cin>>N;
	map<pair<int,int>,int> m;
	UFtree T(N);
	int x=0,y=0,a,b;
	vector<int> ans;
	vector<vector<int>> G(N);
	rep(i,N){
		cin>>a>>b;
		a--,b--;
		m[{a,b}]=i+1;
		m[{b,a}]=i+1;
		if(!T.unite(a,b)){
			ans.push_back(i+1);
			x=a,y=b;
		}else{
			G[a].push_back(b);
			G[b].push_back(a);
		}
	}
	vector<int> order={0},seen(N),pare(N,-1);
	pare[0]=-2;
	rep(i,N){
		a=order[i];
		for(int c:G[a]){
			if(pare[c]==-1){
				pare[c]=a;
				seen[c]=seen[a]+1;
				order.push_back(c);
			}
		}
	}
	if(seen[x]>seen[y]) swap(x,y);
	while(seen[y]!=seen[x]){
		ans.push_back(m[{y,pare[y]}]);
		y=pare[y];
	}
	while(x!=y){
		ans.push_back(m[{y,pare[y]}]);
		y=pare[y];
		ans.push_back(m[{x,pare[x]}]);
		x=pare[x];
	}
	So(ans);
	cout<<ans.size()<<"\n";
	rep(i,(int)ans.size()){
		if(i) cout<<" ";
		cout<<ans[i];
	}
	cout<<"\n";
}
0