結果

問題 No.812 Change of Class
ユーザー face4face4
提出日時 2019-04-13 09:56:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 4,000 ms
コード長 1,637 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 86,544 KB
実行使用メモリ 10,172 KB
最終ジャッジ日時 2023-09-03 14:42:12
合計ジャッジ時間 7,466 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
8,320 KB
testcase_01 AC 14 ms
4,384 KB
testcase_02 AC 47 ms
6,084 KB
testcase_03 AC 100 ms
9,052 KB
testcase_04 AC 88 ms
8,496 KB
testcase_05 AC 221 ms
8,948 KB
testcase_06 AC 217 ms
7,892 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 197 ms
9,216 KB
testcase_10 AC 204 ms
9,616 KB
testcase_11 AC 15 ms
7,148 KB
testcase_12 AC 127 ms
8,956 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 132 ms
7,440 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 123 ms
7,780 KB
testcase_18 AC 92 ms
6,712 KB
testcase_19 AC 105 ms
6,904 KB
testcase_20 AC 198 ms
9,156 KB
testcase_21 AC 86 ms
6,440 KB
testcase_22 AC 39 ms
5,064 KB
testcase_23 AC 7 ms
4,380 KB
testcase_24 AC 11 ms
4,376 KB
testcase_25 AC 30 ms
4,840 KB
testcase_26 AC 161 ms
8,584 KB
testcase_27 AC 143 ms
7,776 KB
testcase_28 AC 103 ms
6,944 KB
testcase_29 AC 22 ms
4,436 KB
testcase_30 AC 132 ms
7,436 KB
testcase_31 AC 112 ms
6,728 KB
testcase_32 AC 49 ms
5,656 KB
testcase_33 AC 136 ms
7,884 KB
testcase_34 AC 10 ms
4,380 KB
testcase_35 AC 23 ms
4,728 KB
testcase_36 AC 10 ms
4,376 KB
testcase_37 AC 59 ms
5,608 KB
testcase_38 AC 5 ms
4,884 KB
testcase_39 AC 59 ms
5,448 KB
testcase_40 AC 15 ms
4,384 KB
testcase_41 AC 4 ms
4,576 KB
testcase_42 AC 129 ms
7,344 KB
testcase_43 AC 32 ms
6,588 KB
testcase_44 AC 87 ms
6,348 KB
testcase_45 AC 9 ms
4,376 KB
testcase_46 AC 30 ms
6,328 KB
testcase_47 AC 87 ms
6,424 KB
testcase_48 AC 93 ms
6,996 KB
testcase_49 AC 24 ms
4,708 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 29 ms
5,088 KB
testcase_52 AC 55 ms
6,820 KB
testcase_53 AC 16 ms
4,436 KB
testcase_54 AC 14 ms
4,380 KB
testcase_55 AC 62 ms
8,060 KB
testcase_56 AC 74 ms
8,916 KB
testcase_57 AC 79 ms
9,608 KB
testcase_58 AC 41 ms
6,408 KB
testcase_59 AC 88 ms
10,172 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:74:58: 警告: ‘day’ may be used uninitialized [-Wmaybe-uninitialized]
   74 |         cout << uf.getSize(a)-1 << " " << (day > 0 ? ceil(log2(day+1)) : 0) << endl;
      |                                                      ~~~~^~~~~~~~~~~~~
main.cpp:64:13: 備考: ‘day’ はここで定義されています
   64 |         int day;
      |             ^~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;

struct UF{
    vector<int> p, rank;
    int n;
    UF(int siz){
        n = siz;
        p.resize(n), rank.resize(n, 1);
        for(int i = 0; i < n; i++)  p[i] = i;
    }

    int parent(int x){
        if(p[x] != x)   p[x] = parent(p[x]);
        return p[x];
    }

    bool same(int x, int y){
        return parent(x) == parent(y);
    }

    void unite(int x, int y){
        x = parent(x), y = parent(y);
        if(x == y)  return;
        if(rank[x] < rank[y]){
            p[x] = y;
            rank[y] += rank[x];
        }else{
            p[y] = x;
            rank[x] += rank[y];
        }
    }

    int getSize(int x){
        return rank[parent(x)];
    }
};

int main(){
    int n, m;
    cin >> n >> m;
    vector<int> path[n];
    UF uf(n);
    while(m-- > 0){
        int p, q;
        cin >> p >> q;
        p--, q--;
        uf.unite(p, q);
        path[p].push_back(q);
        path[q].push_back(p);
    }
    int q;
    cin >> q;
    while(q-- > 0){
        int a;
        cin >> a;
        a--;
        vector<bool> visit(n, 0);
        queue<pair<int,int>> q;
        q.push({a, -1});
        int day;
        while(!q.empty()){
            pair<int,int> p = q.front();    q.pop();
            if(visit[p.first])  continue;
            visit[p.first] = true;
            day = p.second;
            for(int next : path[p.first]){
                if(!visit[next])    q.push({next, day+1});
            }
        }
        cout << uf.getSize(a)-1 << " " << (day > 0 ? ceil(log2(day+1)) : 0) << endl;
    }
    return 0;
}
0