結果

問題 No.1254 補強への架け橋
ユーザー okok
提出日時 2020-10-09 23:03:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 1,963 bytes
コンパイル時間 1,109 ms
コンパイル使用メモリ 110,676 KB
実行使用メモリ 34,420 KB
最終ジャッジ日時 2023-09-27 19:23:22
合計ジャッジ時間 12,940 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1 ms
4,384 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,384 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 3 ms
4,380 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 1 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 10 ms
4,980 KB
testcase_64 AC 4 ms
4,376 KB
testcase_65 AC 7 ms
4,380 KB
testcase_66 AC 6 ms
4,380 KB
testcase_67 AC 3 ms
4,376 KB
testcase_68 AC 6 ms
4,376 KB
testcase_69 AC 8 ms
4,616 KB
testcase_70 AC 5 ms
4,380 KB
testcase_71 AC 3 ms
4,380 KB
testcase_72 AC 8 ms
4,712 KB
testcase_73 AC 4 ms
4,380 KB
testcase_74 AC 7 ms
4,568 KB
testcase_75 AC 6 ms
4,380 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 6 ms
4,380 KB
testcase_78 AC 11 ms
4,872 KB
testcase_79 AC 10 ms
5,128 KB
testcase_80 AC 8 ms
4,636 KB
testcase_81 AC 10 ms
4,948 KB
testcase_82 AC 9 ms
4,808 KB
testcase_83 AC 144 ms
20,556 KB
testcase_84 AC 143 ms
20,032 KB
testcase_85 AC 72 ms
13,560 KB
testcase_86 AC 116 ms
17,844 KB
testcase_87 AC 132 ms
19,236 KB
testcase_88 AC 15 ms
5,652 KB
testcase_89 AC 161 ms
20,404 KB
testcase_90 AC 83 ms
14,064 KB
testcase_91 AC 62 ms
12,108 KB
testcase_92 AC 30 ms
7,860 KB
testcase_93 AC 105 ms
17,224 KB
testcase_94 AC 101 ms
15,776 KB
testcase_95 AC 98 ms
15,676 KB
testcase_96 AC 155 ms
19,472 KB
testcase_97 AC 53 ms
10,756 KB
testcase_98 AC 142 ms
19,732 KB
testcase_99 AC 75 ms
13,424 KB
testcase_100 AC 150 ms
20,744 KB
testcase_101 AC 26 ms
7,316 KB
testcase_102 AC 13 ms
5,384 KB
testcase_103 AC 28 ms
7,944 KB
testcase_104 AC 41 ms
9,440 KB
testcase_105 AC 108 ms
16,976 KB
testcase_106 AC 55 ms
11,188 KB
testcase_107 AC 144 ms
20,592 KB
testcase_108 AC 141 ms
20,208 KB
testcase_109 AC 108 ms
16,864 KB
testcase_110 AC 90 ms
15,416 KB
testcase_111 AC 102 ms
16,564 KB
testcase_112 AC 38 ms
9,120 KB
testcase_113 AC 86 ms
15,124 KB
testcase_114 AC 54 ms
10,896 KB
testcase_115 AC 17 ms
5,980 KB
testcase_116 AC 67 ms
12,308 KB
testcase_117 AC 40 ms
9,064 KB
testcase_118 AC 136 ms
19,640 KB
testcase_119 AC 71 ms
13,160 KB
testcase_120 AC 128 ms
18,940 KB
testcase_121 AC 32 ms
8,520 KB
testcase_122 AC 63 ms
12,372 KB
testcase_123 AC 1 ms
4,380 KB
testcase_124 AC 254 ms
34,416 KB
testcase_125 AC 226 ms
34,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include<deque>
#include<map>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


bool cycle_detection(const vector<vector<int>> &G, vector<bool> &used, vector<bool> &used2, deque<int> &C, int cur, int per){
	used[cur] = true;
	used2[cur] = true;
	
	C.push_back(cur);
    
	for(int nex : G[cur]){
		if(used[nex]){
			if(used2[nex]) { 
                if(per == nex) continue;
                
				while(C.front() != nex){
					C.pop_front();
				}
				return true;
			} else {
				continue;
			}
		}
		
		if(cycle_detection(G, used, used2, C, nex, cur)) return true;
	}
	
	C.pop_back();
	
	used2[cur] = false;
	
	return false;
}

deque<int> cycle_detection(const vector<vector<int>> &G){
	int N = G.size();
	vector<bool> used(N), used2(N);
	deque<int> C;
	
	for(int i = 0; i < N; i++){
		if(used[i] == true) continue;
		C.clear();
		if(cycle_detection(G, used, used2, C, i, -1)) return C;
	}
	
	return {};
}

signed main(){
	cout<<fixed<<setprecision(10);
	
	int N;
    vector<vector<int>> G;
    deque<int> c;
    map<pair<int,int>, int> mp;
    
    cin>>N;
	
    G.resize(N);
    
	for(int i = 0; i < N; i++){
        int a, b;
        
        cin>>a>>b;
        
        a--, b--;
        
        G[a].push_back(b);
        G[b].push_back(a);
        
        mp[{a,b}] = i + 1;
        mp[{b,a}] = i + 1;
    }
    
    c = cycle_detection(G);
    
    cout<<(int)c.size()<<endl;
    
    for(int i = 0; i < c.size(); i++){
        if(i) cout<<" ";
        cout<<mp[{c[i], c[(i+1)%c.size()]}];
    }
    cout<<endl;
    
    // for(int i = 0; i< c.size(); i++){
        // cout<<" "<<c[i];
    // }
    // cout<<endl;
	return 0;
}
0