結果

問題 No.2072 Anatomy
ユーザー k1suxuk1suxu
提出日時 2022-09-16 22:17:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,751 bytes
コンパイル時間 4,815 ms
コンパイル使用メモリ 286,980 KB
実行使用メモリ 10,444 KB
最終ジャッジ日時 2023-08-23 15:57:52
合計ジャッジ時間 7,366 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 53 ms
9,068 KB
testcase_10 WA -
testcase_11 AC 55 ms
8,796 KB
testcase_12 AC 44 ms
7,656 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 34 ms
6,936 KB
testcase_16 WA -
testcase_17 AC 56 ms
9,308 KB
testcase_18 AC 29 ms
6,196 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 55 ms
9,424 KB
testcase_22 WA -
testcase_23 AC 58 ms
10,076 KB
testcase_24 AC 54 ms
9,332 KB
testcase_25 WA -
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 57 ms
9,264 KB
testcase_28 AC 52 ms
9,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define pb push_back
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vs vector<string>
#define vvs vector<vs>
#define vc vector<char>
#define vvc vector<vc>
#define pii pair<int,int>
#define pllll pair<ll,ll>
#define vpii vector<pair<int,int>>
#define vpllll vector<pair<ll,ll>>
#define vpis vector<pair<int,string>>
#define vplls vector<pair<ll, string>>
#define vpsi vector<pair<string, int>>
#define vpsll vector<pair<string, ll>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, -1, 0, 1, -1, 1, -1, 1};

#define int long long

//普通UnionFind -- Disjoing Set Union
struct UnionFind {
    vector<int> r;
    
    UnionFind(int n) {
        r = vector<int>(n, -1);
    }

    int root(int x) {
        if(r[x] < 0) return x;
        return r[x] = root(r[x]);
    }

    bool unite(pair<int, int> p) {
        return unite(p.first, p.second);
    }

    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if(x == y) return false;
        if(r[x] > r[y]) swap(x, y);
        r[x] += r[y];
        r[y] = x;
        return true;
    }

    bool issame(pair<int, int> p) {
        return issame(p.first, p.second);
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return -r[root(x)];
    }

    int number_of_set() {
        set<int> st;
        FOR(r.size()) st.insert(root(i));
        return st.size();
    }
};

struct StronglyConnectedComponents {
    int n;
    //reversed_graph
    //辺を逆向きに貼りなおしたグラフ。
    vector<vector<int>> graph, reversed_graph;
    vector<int> order, component;
    vector<bool> used;
    void dfs(int v) {
        used[v] = 1;
        for(auto e : graph[v]) {
            if(used[e]) continue;
            dfs(e);
        }
        order.push_back(v);
    }
    void dfs2(int v, int k) {
        component[v] = k;
        for(auto e : reversed_graph[v]) {
            if(component[e] == -1) dfs2(e, k);
        }
    }

    StronglyConnectedComponents(vector<vector<int>> &G_) {
        n = G_.size();
        graph = G_;
        reversed_graph.resize(n);
        component.assign(n, -1);
        used.resize(n);
        for(int v = 0; v < n; v++) {
            for(auto e : graph[v]) reversed_graph[e].push_back(v);
        }
        for(int v = 0; v < n; v++) if(!used[v]) dfs(v);
        int k = 0;
        //topoogical sort in all the sub trees
        reverse(order.begin(), order.end());
        //いままでの強連結成分に含まれていなかったらDFS
        for(auto e : order) if(component[e] == -1) dfs2(e, k), k++;
    }

    //return if vertex(u, v) in same strongly connected component
    bool issame(int u, int v) {
        return component[u] == component[v];
    }

    vector<vector<int>> rebuild() {
        //コンポーネント数
        const int N = *max_element(component.begin(), component.end()) + 1;
        vector<vector<int>> rebuilded_graph(N);
        set<pair<int, int>> connected;
        for(int v = 0; v < N; v++) {
            for(auto e : graph[v]) {
                //同じ強連結成分に含まれていなくてかつ
                //その二つの強連結成分がグラフ上でつながれていなかったら
                if(component[v] != component[e] && !connected.count(make_pair(v, e))) {
                    connected.insert(make_pair(v, e));
                    rebuilded_graph[component[v]].push_back(component[e]);
                }
            }
        }
        return rebuilded_graph;
    }

    vector<vector<int>> pull_groups() {
        const int N = *max_element(component.begin(), component.end()) + 1;
        vector<vector<int>> groups;
        
        for(int i = 0; i < n; i++) {
            groups[component[i]].push_back(i);
        }

        return groups;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    vpii edges;
    FOR(m) {
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        edges.emplace_back(u, v);
    }

    UnionFind UF(n);
    vector<bool> done(n, false);
    vector<int> ans(n, 1);
    vector<int> ids;
    reverse(all(edges));

    for(auto e : edges) {
        int r1 = UF.root(e.first);
        int r2 = UF.root(e.second);

        if(done[r1] || done[r2]) {
            int nans = max({ans[r1], ans[r2], ans[e.first], ans[e.second]});
            ans[r1] = nans+1;
            ans[r2] = nans+1;
            ans[e.first] = nans+1;
            ans[e.second] = nans+1;
            for(auto e : ids) {
                done[e] = false;
            }
            ids.clear();
        }

        done[r1] = true;
        done[r2] = true;
        done[e.first] = true;
        done[e.second] = true;
        ids.push_back(r1);
        ids.push_back(r2);
        ids.push_back(e.first);
        ids.push_back(e.second);
        UF.unite(e);
    }

    cout << *max_element(all(ans)) << endl;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0