답안 #946045

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
946045 2024-03-14T09:51:06 Z sysia 철인 이종 경기 (APIO18_duathlon) C++17
0 / 100
105 ms 29456 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;

struct DSU{
    vector<int>rep, sz;
    
    DSU(int n){
        rep.assign(n+1, 0);
        iota(rep.begin(), rep.end(), 0);
        sz.assign(n+1, 1);
    }

    int f(int a){
        return a == rep[a] ? a : rep[a] = f(rep[a]);
    }

    void u(int a, int b){
        a = f(a); b = f(b);
        if (a == b) return;
        if (sz[a] < sz[b]) swap(a, b);
        sz[a] += sz[b];
        rep[b] = a;
    }
};

void solve(){
    int n, m; cin >> n >> m;//nikt nie powiedzial ze graf jest spojny xd
    vector<vector<T>>g(n+1);
    vector<T>edges;
    vector<bool>skip(m);
    for (int i = 0; i<m; i++){
        int a, b; cin >> a >> b;
        g[a].emplace_back(b, i);
        g[b].emplace_back(a, i);
        edges.emplace_back(a, b);
    }
    vector<int>low(n+1), pre(n+1);
    int tt = 0;
    vector<bool>vis(n+1);
    function<void(int, int)>dfs = [&](int v, int pa){
        pre[v] = ++tt;
        low[v] = pre[v];
        vis[v] = 1;
        for (auto [x, i]: g[v]){
            if (x == pa) continue;
            if (vis[x]){
                low[v] = min(low[v], pre[x]);
            } else {
                // debug(v, x);
                dfs(x, v);
                low[v] = min(low[v], low[x]);
                // debug(v, pre[v], low[x]);
                if (pre[v] < low[x]){
                    debug(x, v);
                    skip[i] = 1;
                }
            }
        }
    };
    for (int i = 1; i<=n; i++){
        if (!vis[i]){
            dfs(i, i);
        }
    }
    DSU dsu(n+1);
    for (int i = 0; i<m; i++){
        if (!skip[i]){
            dsu.u(edges[i].first, edges[i].second);
        }
    }
    vector<vector<int>>G(n+1);
    vector<int>sz(n+1);
    for (int i = 0; i<m; i++){
        if (!skip[i]) continue;
        //most
        auto [a, b] = edges[i];
        a = dsu.f(a);b = dsu.f(b);
        debug(a, b);
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    for (int i = 1; i<=n; i++){
        sz[dsu.f(i)] = dsu.sz[dsu.f(i)];
    }
    debug(sz);
    int ans = 0, ans2 = 0;
    vis.assign(n+1, 0);
    function<void(int, int)>dfessa = [&](int v, int pa){
        int sq = 0, s = 0;
        vis[v] = 1;
        for (auto x: G[v]){
            if (x == pa) continue;
            dfessa(x, v);
            sq += sz[x] * sz[x];
            s += sz[x];
        }
        //do rodzica
        sq += (n-s-sz[v])*(n-s-sz[v]);
        s = n-sz[v];
        debug(v, 2 * sz[v] * (sz[v]-1) * s);
        ans += 2 * sz[v] * (s*s-sq)/2; //jeden znajduje sie tutaj
        ans += 2 * (sz[v]-2) * (sz[v]-1) * s; //dwa znajduja sie w jednej spojnej
        ans += 2 * s * (sz[v]-1);
        ans += sz[v] * (sz[v]-1) * (sz[v]-2);
        debug(v, ans);
        for (auto x: G[v]){
            if (x != pa){
                sz[v] += sz[x];
            }
        }
    };
    for (int i = 1; i<=n;i++){
        if (dsu.f(i) == i && !vis[i]){
            debug(i);
            dfessa(i, i);
        }
    }
    debug(ans, ans2);
    cout << ans << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}

Compilation message

count_triplets.cpp: In function 'void solve()':
count_triplets.cpp:114:18: warning: unused variable 'ans2' [-Wunused-variable]
  114 |     int ans = 0, ans2 = 0;
      |                  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 28384 KB Output is correct
2 Correct 68 ms 29456 KB Output is correct
3 Incorrect 105 ms 24904 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 500 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Incorrect 1 ms 600 KB Output isn't correct
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 19292 KB Output is correct
2 Correct 58 ms 20600 KB Output is correct
3 Correct 62 ms 20676 KB Output is correct
4 Correct 57 ms 20580 KB Output is correct
5 Correct 54 ms 20780 KB Output is correct
6 Correct 74 ms 26628 KB Output is correct
7 Correct 71 ms 25280 KB Output is correct
8 Correct 77 ms 24000 KB Output is correct
9 Correct 70 ms 22720 KB Output is correct
10 Incorrect 65 ms 20568 KB Output isn't correct
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Incorrect 1 ms 600 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 19400 KB Output is correct
2 Correct 57 ms 20404 KB Output is correct
3 Incorrect 85 ms 20420 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -