結果

問題 No.1254 補強への架け橋
ユーザー 沙耶花沙耶花
提出日時 2020-10-09 21:55:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 3,074 bytes
コンパイル時間 2,529 ms
コンパイル使用メモリ 213,640 KB
実行使用メモリ 16,124 KB
最終ジャッジ日時 2024-07-20 10:59:53
合計ジャッジ時間 12,619 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 1 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 3 ms
5,376 KB
testcase_53 AC 2 ms
5,376 KB
testcase_54 AC 3 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 2 ms
5,376 KB
testcase_57 AC 3 ms
5,376 KB
testcase_58 AC 2 ms
5,376 KB
testcase_59 AC 2 ms
5,376 KB
testcase_60 AC 2 ms
5,376 KB
testcase_61 AC 2 ms
5,376 KB
testcase_62 AC 2 ms
5,376 KB
testcase_63 AC 10 ms
5,376 KB
testcase_64 AC 4 ms
5,376 KB
testcase_65 AC 6 ms
5,376 KB
testcase_66 AC 6 ms
5,376 KB
testcase_67 AC 4 ms
5,376 KB
testcase_68 AC 6 ms
5,376 KB
testcase_69 AC 8 ms
5,376 KB
testcase_70 AC 4 ms
5,376 KB
testcase_71 AC 4 ms
5,376 KB
testcase_72 AC 8 ms
5,376 KB
testcase_73 AC 4 ms
5,376 KB
testcase_74 AC 8 ms
5,376 KB
testcase_75 AC 7 ms
5,376 KB
testcase_76 AC 3 ms
5,376 KB
testcase_77 AC 6 ms
5,376 KB
testcase_78 AC 9 ms
5,376 KB
testcase_79 AC 9 ms
5,376 KB
testcase_80 AC 10 ms
5,376 KB
testcase_81 AC 9 ms
5,376 KB
testcase_82 AC 8 ms
5,376 KB
testcase_83 AC 92 ms
15,540 KB
testcase_84 AC 85 ms
15,128 KB
testcase_85 AC 52 ms
10,728 KB
testcase_86 AC 73 ms
13,512 KB
testcase_87 AC 98 ms
14,456 KB
testcase_88 AC 13 ms
5,376 KB
testcase_89 AC 88 ms
15,332 KB
testcase_90 AC 57 ms
11,044 KB
testcase_91 AC 45 ms
9,516 KB
testcase_92 AC 25 ms
6,500 KB
testcase_93 AC 70 ms
13,084 KB
testcase_94 AC 65 ms
12,112 KB
testcase_95 AC 63 ms
12,080 KB
testcase_96 AC 86 ms
14,728 KB
testcase_97 AC 37 ms
8,532 KB
testcase_98 AC 83 ms
14,948 KB
testcase_99 AC 53 ms
10,372 KB
testcase_100 AC 91 ms
15,728 KB
testcase_101 AC 23 ms
6,156 KB
testcase_102 AC 12 ms
5,376 KB
testcase_103 AC 24 ms
6,528 KB
testcase_104 AC 32 ms
7,752 KB
testcase_105 AC 73 ms
12,964 KB
testcase_106 AC 41 ms
9,048 KB
testcase_107 AC 88 ms
15,340 KB
testcase_108 AC 91 ms
15,208 KB
testcase_109 AC 72 ms
12,868 KB
testcase_110 AC 64 ms
12,020 KB
testcase_111 AC 68 ms
12,636 KB
testcase_112 AC 35 ms
7,556 KB
testcase_113 AC 61 ms
11,692 KB
testcase_114 AC 41 ms
8,888 KB
testcase_115 AC 15 ms
5,376 KB
testcase_116 AC 49 ms
9,724 KB
testcase_117 AC 30 ms
7,588 KB
testcase_118 AC 84 ms
14,784 KB
testcase_119 AC 50 ms
10,288 KB
testcase_120 AC 85 ms
14,368 KB
testcase_121 AC 31 ms
6,952 KB
testcase_122 AC 45 ms
9,624 KB
testcase_123 AC 2 ms
5,376 KB
testcase_124 AC 107 ms
16,124 KB
testcase_125 AC 114 ms
16,124 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:132:17: warning: 'z' may be used uninitialized [-Wmaybe-uninitialized]
  132 |                 if(i==z)ans.push_back(i);
      |                 ^~
main.cpp:108:13: note: 'z' was declared here
  108 |         int z;
      |             ^
main.cpp:129:20: warning: 'y' may be used uninitialized [-Wmaybe-uninitialized]
  129 |         int d = a[y];
      |                    ^
main.cpp:107:15: note: 'y' was declared here
  107 |         int x,y;
      |               ^
main.cpp:127:32: warning: 'x' may be used uninitialized [-Wmaybe-uninitialized]
  127 |         vector<int> a = get(x,E),b = get(y,E);
      |                                ^
main.cpp:107:13: note: 'x' was declared here
  107 |         int x,y;
      |             ^

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

}  // namespace atcoder

using namespace atcoder;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 2000000001

vector<int> get(int x,vector<vector<int>> E){
	vector<int> ret(E.size(),Inf);
	ret[x] = 0;
	queue<int> Q;
	Q.push(x);
	
	while(Q.size()!=0){
		int u = Q.front();
		Q.pop();
		rep(i,E[u].size()){
			int v = E[u][i];
			if(ret[v]!=Inf)continue;
			ret[v] = ret[u]+1;
			Q.push(v);
		}
	}
	return ret;
}

int main(){	

	int n;
	cin>>n;
	
	dsu D(n);
	vector<int> u(n),v(n);
	int x,y;
	int z;
	vector<vector<int>> E(n,vector<int>());
	rep(i,n){
		cin>>u[i]>>v[i];
		u[i]--;
		v[i]--;
		
		if(D.same(u[i],v[i])){
			x = u[i];
			y = v[i];
			z = i;
		}
		else{
			D.merge(u[i],v[i]);
			E[u[i]].push_back(v[i]);
			E[v[i]].push_back(u[i]);
		}
	}
	
	vector<int> a = get(x,E),b = get(y,E);
	
	int d = a[y];
	vector<int> ans;
	rep(i,n){
		if(i==z)ans.push_back(i);
		else{
			if(a[u[i]]+b[v[i]]+1==d || b[u[i]]+a[v[i]]+1==d)ans.push_back(i);
		}
	}
	
	cout<<ans.size()<<endl;
	rep(i,ans.size()){
		if(i!=0)cout<<' ';
		cout<<ans[i]+1;
	}
	cout<<endl;

    return 0;
}
0