結果

問題 No.2072 Anatomy
ユーザー
提出日時 2022-09-16 22:37:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 5,053 bytes
コンパイル時間 1,101 ms
コンパイル使用メモリ 112,640 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-12-21 21:55:09
合計ジャッジ時間 4,727 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 112 ms
7,936 KB
testcase_09 AC 125 ms
8,320 KB
testcase_10 AC 115 ms
7,552 KB
testcase_11 AC 123 ms
8,064 KB
testcase_12 AC 95 ms
7,040 KB
testcase_13 AC 126 ms
8,576 KB
testcase_14 AC 80 ms
6,528 KB
testcase_15 AC 77 ms
6,528 KB
testcase_16 AC 126 ms
8,576 KB
testcase_17 AC 126 ms
8,448 KB
testcase_18 AC 66 ms
6,144 KB
testcase_19 AC 131 ms
8,704 KB
testcase_20 AC 130 ms
8,704 KB
testcase_21 AC 126 ms
8,704 KB
testcase_22 AC 131 ms
8,704 KB
testcase_23 AC 128 ms
8,832 KB
testcase_24 AC 127 ms
8,704 KB
testcase_25 AC 129 ms
8,704 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 127 ms
8,704 KB
testcase_28 AC 124 ms
8,832 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:168:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  168 |         auto [a,b] = m[i];
      |              ^

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <cmath>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cassert>
using namespace std;
namespace atcoder{};using namespace atcoder;

using ull = uint64_t;
using ll = int64_t;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;
using ld = long double;
using P = pair<ll,ll>;
using G = vector<vector<int>>;

#ifdef MYDEBUG
#define LO(...) fprintf(stderr, __VA_ARGS__)
#define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" << endl
#else
#define LO(...) (void)0
#define debug(x) (void)0
#endif

#define reps(i,a,n) for(ll i##_len = (ll)(n), i = (a); i < i##_len; ++i)
#define rep(i,n) reps(i,0,n)
#define rrep(i,n) reps(i,1,n+1)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define rrepd(i,n) for(ll i=n;i>=1;i--)

#define inp(i) ll i; cin >> i;
#define inps(s) string s; cin >> s;
#define inpp(p) cin >> (p).first >> (p).second;
#define inpv(v,N) vll v(N);rep(i,N)cin>>v[i];
#define inpg(g,N,M) g.resize(N);rep(i,M){inp(a);inp(b);a--,b--;g[a].push_back(b);g[b].push_back(a);}
#define all(v) begin(v),end(v)
#define YESNO(bool) if(bool){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(bool) if(bool){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl

#define SP cout << " ";
#define ENDL cout << "\n";
#define outp(i) cout << (i);
#define outps(i) cout << (i) << " ";
#define outpl(i) cout << (i) << "\n";
#define setfp() cout << fixed << setprecision(16)

template<typename C>
void outpv(C &v){for(auto &&e:v){cout << e;if(&e != &v.back()) cout << ' ';}cout << "\n";}

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

inline ll lg(ll __n) { return sizeof(ll) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

#ifndef ATCODER_DSU_HPP
#define ATCODER_DSU_HPP 1

#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) {}
    explicit 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

#endif  // ATCODER_DSU_HPP

vector<P> m;
int main(){
    inp(N);
    inp(M);
    dsu d(N);
    m.resize(M);
    rep(i,M){
        inp(a);inp(b);
        a--,b--;
        m[i] = {a,b};
    }

    ll c = 0;
    vll ff(N);
    repd(i,M){
        auto [a,b] = m[i];
        LO("i=%ld, %ld %ld\n",i,a,b);
        ll p = ff[d.leader(a)];
        ll q = ff[d.leader(b)];
        if(d.same(m[i].first, m[i].second)){
            c++;
            LO("same: %ld==%ld  max:%ld\n", a,b,max(p,q));
            ff[d.leader(a)]++;
        }else{
            d.merge(m[i].first, m[i].second);
            LO("merge: %ld<>%ld  max:%ld\n", a,b,max(p,q));
            ff[d.leader(a)] = max(p,q)+1;
        }
    }

    outpl(ff[d.leader(0)]);

    return 0;
}
0