Submission #7867414


Source Code Expand

Copy
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <string>
#include <queue>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#define ll long long
#define fr(i,k,N) for(decltype(N+k) i=k; i<N;i++)
#define rep(i,N) for(decltype(N) i=0; i<N;i++)
#define ALL(v) v.begin(),v.end()
using namespace std;
template <class X> void pr(X test){cout<<test<<endl;}
template <class X> void prr(X test){for(auto it:test){cout<<it<<endl;}}
template <class X> void prrr(X test){int f=0;for(auto it:test){cout<<(f++!=0?" ":"")<<it;}cout<<endl;}
template<typename X>
istream&operator>>(istream&i,vector<X>&v){for(X&x:v)i>>x;return i;}

class UF { // union find from https://github.com/kartikkukreja/blog-codes (MIT)
    // [usage]
    // auto uf = new UF(n);
    // uf->merge(l, r); // 0 <= l, r <= n-1
    // ...
public:
    int *id; // inner id
    int cnt; // number of all vartices
    int *sz; // sz[i] means size of i's set
    UF(int N)   {
        cnt = N;
        id = new int[N];
        sz = new int[N];
        for(int i=0; i<N; i++) {
            id[i] = i;
            sz[i] = 1;
        }
    }
    ~UF() {
        delete [] id;
        delete [] sz;
    }
    int find(int p) {
        int root = p;
        while (root != id[root]) root = id[root];
        while (p != root) {
            int newp = id[p];
            id[p] = root;
            p = newp;
        }
        return root;
    }
    void merge(int x, int y) {
        int i = find(x);
        int j = find(y);
        if (i == j) return;
        
        if (sz[i] < sz[j]) {
            id[i] = j;
            sz[j] += sz[i];
        } else {
            id[j] = i;
            sz[i] += sz[j];
        }
        cnt--;
    }
    bool connected(int x, int y) {
        return find(x) == find(y);
    }
    int count() {
        return cnt;
    }
};

// return 約数unordered_set 1 <= x <= n
auto divv = [](auto n) {
  unordered_set<decltype(n)> v;
  for(decltype(n) i = 1; i*i <= n; i++) {
    if(n%i == 0) {
      v.insert(i);
      if(i*i != n) v.insert(n/i);
    }
  }
  return v;
};

// check prime (and n>=2) or not
auto isPrime = [](auto n){
  if(n <= 1) return false;
  for(decltype(n) i = 2; i <= sqrt(n); ++i) {
      if(n % i == 0) {
          return false;
      }
  }
  return true;
};

int main() {
    ll n;
    cin>>n;
    unordered_map<ll,vector<ll>> m;
    rep(j,n){
        string s;
        cin>>s;
        for(ll i =0;i<n;i++){
            if(s[i]=='1') m[i+1].push_back(j+1);
        }
    }
    {
        auto s = (*m.begin()).first;
        queue<ll> q;
        q.push(s);
        unordered_map<ll, ll> clas;
        clas[s]=1;
        while(!q.empty()){
            auto a = q.front();
            q.pop();
            for(auto it:m[a]){
                if(clas[it]==0) {
                    clas[it]=clas[a]+1;
                    q.push(it);
                }
            }
        }
        //check
        for(auto it:m) for(auto jt:it.second){
            if(clas[it.first]==clas[jt]){
                pr(-1);
                return 0;
            }
        }
        ll arg =1,mx = clas[1];
        for(auto it:clas){
            if(it.second > mx){
                mx = it.second;
                arg = it.first;
            }
        }
        clas.clear();
        queue<ll> qq;
        qq.push(arg);
        clas[arg]=1; // re
        while(!qq.empty()){
            auto a = qq.front();
            qq.pop();
            for(auto it:m[a]){
                if(clas[it]==0) {
                    clas[it]=clas[a]+1;
                    qq.push(it);
                }
            }
        }
        for(auto it:clas){
            if(it.second > mx){
                mx = it.second;
            }
        }
        pr(mx);
    }
}

Submission Info

Submission Time
Task B - Graph Partition
User quiye
Language C++14 (GCC 5.4.1)
Score 0
Code Size 4106 Byte
Status WA
Exec Time 4 ms
Memory 640 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 54
WA × 1
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt WA 3 ms 256 KB
02.txt AC 3 ms 256 KB
03.txt AC 3 ms 256 KB
04.txt AC 3 ms 256 KB
05.txt AC 3 ms 256 KB
06.txt AC 3 ms 256 KB
07.txt AC 3 ms 256 KB
08.txt AC 3 ms 256 KB
09.txt AC 3 ms 256 KB
10.txt AC 3 ms 256 KB
11.txt AC 3 ms 256 KB
12.txt AC 3 ms 256 KB
13.txt AC 3 ms 256 KB
14.txt AC 3 ms 256 KB
15.txt AC 3 ms 256 KB
16.txt AC 3 ms 256 KB
17.txt AC 3 ms 256 KB
18.txt AC 3 ms 256 KB
19.txt AC 3 ms 256 KB
20.txt AC 3 ms 256 KB
21.txt AC 3 ms 256 KB
22.txt AC 3 ms 256 KB
23.txt AC 3 ms 256 KB
24.txt AC 3 ms 256 KB
25.txt AC 3 ms 256 KB
26.txt AC 3 ms 256 KB
27.txt AC 3 ms 256 KB
28.txt AC 3 ms 256 KB
29.txt AC 3 ms 256 KB
30.txt AC 3 ms 256 KB
31.txt AC 3 ms 256 KB
32.txt AC 3 ms 256 KB
33.txt AC 3 ms 256 KB
34.txt AC 3 ms 256 KB
35.txt AC 4 ms 640 KB
36.txt AC 4 ms 640 KB
37.txt AC 4 ms 512 KB
38.txt AC 4 ms 512 KB
39.txt AC 3 ms 256 KB
40.txt AC 3 ms 256 KB
41.txt AC 3 ms 256 KB
42.txt AC 3 ms 256 KB
43.txt AC 3 ms 256 KB
44.txt AC 3 ms 256 KB
45.txt AC 3 ms 256 KB
46.txt AC 3 ms 256 KB
47.txt AC 3 ms 256 KB
48.txt AC 3 ms 256 KB
49.txt AC 1 ms 256 KB
50.txt AC 1 ms 256 KB
51.txt AC 1 ms 256 KB
52.txt AC 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB
s3.txt AC 1 ms 256 KB