結果

問題 No.826 連絡網
ユーザー kappybarkappybar
提出日時 2020-04-12 11:50:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,342 bytes
コンパイル時間 2,070 ms
コンパイル使用メモリ 172,260 KB
実行使用メモリ 8,744 KB
最終ジャッジ日時 2023-10-22 00:57:20
合計ジャッジ時間 6,405 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,872 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 28 ms
4,372 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll =  long long ;
using P = pair<ll,ll> ;
const int INF = 1e9;
const int MOD = 1000000007;

ll gcd(ll i,ll j){
        if(j == 0) return i;
        return gcd(j,i%j);
}

struct Unionfind{
    vector<int> parents;
    int m;
    Unionfind(int n):parents(n,-1){
        m = n;
    }

    int find(int x){
        if(parents.at(x) < 0){
            return x;
        }else{
            parents.at(x) = find(parents.at(x));
            return parents.at(x);
        }
    }

    void unite(int x,int y){
        x = find(x);
        y = find(y);

        if(x==y) return;
        if(parents.at(x) > parents.at(x))swap(x,y);
        parents.at(x) += parents.at(y);
        parents.at(y) = x;
    }  

   int size(int x){
       return -parents.at(find(x));
   }

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

   vector<int> members(int x){
       int root = find(x);
       vector<int> A;
       for(int i=0;i<m;i++){
           if(find(i) ==root ){
               A.push_back(i);
           }
       }
      return A;
  }
};

int main(){
    int n,p;
    cin >> n >> p;
    --p;
    Unionfind uf(n);
    rep(i,n)rep(j,n){
        if(gcd(i+1,j+1)==1) continue;
        uf.unite(i,j);
    }
    cout << uf.size(p) << endl;
    return 0;
}
0