답안 #897583

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
897583 2024-01-03T12:52:59 Z davitmarg 철인 이종 경기 (APIO18_duathlon) C++17
31 / 100
209 ms 122568 KB
//DavitMarg

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <random>
#include <chrono>
#define mod 998244353ll
#define LL long long
#define LD long double
#define MP make_pair    
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;

const int N = 500005;


int n, m, used[N], art[N];
int a[N], b[N];
vector<pair<int,int>> g[N];
int tin[N], tout[N], low[N], timer;
int color[N], col, cnt[N], num, comp[N];
set<int> s[N], colors[N];
vector<int> st;

void addComp(int ind = 0)
{
    if (st.empty())
        return;
    col++;
    while (!st.empty())
    {
        int j = st.back();
        s[col].insert(a[j]);
        s[col].insert(b[j]);

        colors[a[j]].insert(col);
        colors[b[j]].insert(col);

        st.pop_back();
        if (j == ind)
            break;
    }
}

void dfs(int v, int p)
{
    tin[v] = ++timer;
    used[v] = 1;
    low[v] = tin[v];
    comp[v] = num;
    cnt[num]++;
    int children = 0;
    for (int j = 0; j < g[v].size(); j++)
    {
        int to = g[v][j].first;
        int ind = g[v][j].second;

        if (to == p)
            continue;
        if (used[to])
        {
            low[v] = min(low[v], tin[to]);
            if(low[to] < tin[v])
                st.push_back(ind);
            continue;
        }
        children++;
        st.push_back(ind);
        dfs(to, v);

        if (low[to] >= tin[v])
        {
            art[v] = 1;
            addComp(ind);
        }

        low[v] = min(low[v], low[to]);
    }

    if (p == 0)
    {
        art[v] = (children > 1);
        addComp();
    }
}


int k;
LL sz[N], sb[N], ans, sum;
vector<int> G[N];

void dfs2(int v, int p)
{
    used[v] = 1;
    sb[v] = sz[v];
    vector<LL> sizes;
    for (int to : G[v])
    {
        if (to == p)
            continue;
        dfs2(to, v);
        sb[v] += sb[to];
        sizes.push_back(sb[to]);
    }

    LL psize = sum - sb[v];
    if (psize)
        sizes.push_back(psize);

    if (v <= k)
    {
        LL pref = 0;
        for (int i = 0; i < sizes.size(); i++)
        {
            ans += pref * sizes[i] * 2ll;
            pref += sizes[i];
        }
    }
    else
    { 
        LL x = sz[v];
        LL y = G[v].size();

        LL pref = 0;
        for (int i = 0; i < sizes.size(); i++)
        {
            ans += pref * sizes[i] * 2ll * x;
            pref += sizes[i];
        }

        for (int i = 0; i < sizes.size(); i++)
        {
            ans += (sizes[i] - 1ll) * x * (x - 1ll) * 2;
        }
    }
    
}


void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> a[i] >> b[i];
        g[a[i]].push_back(MP(b[i], i));
        g[b[i]].push_back(MP(a[i], i));
    }

    for(int i = 1; i <= n; i++)
        if (!used[i])
        {
            num++;
            dfs(i, 0);
        }

    vector<int> ar;

    for (int i = 1; i <= n; i++)
    {
        if (art[i])
            ar.push_back(i);
        used[i] = 0;
    }

    k = ar.size();

    for (int i = 1; i <= col; i++)
    {
        LL x = s[i].size();
        sz[k + i] = s[i].size();
        ans += x * (x - 1ll) * (x - 2ll);
    }

    for (int j = 0; j < k; j++)
    {
        int v = ar[j];

        int x = j + 1;

        sz[x] = 1ll;
        for (auto it = colors[v].begin(); it != colors[v].end(); ++it)
        {
            int y = (*it) + k;
            sz[y]--;

            G[x].push_back(y);
            G[y].push_back(x);
        }
        
    }


    for (int i = 1; i <= k; i++)
    {
        if (used[i])
            continue;
        int v = ar[i - 1];
        sum = cnt[comp[v]];
        dfs2(i, 0);
    }

    cout << ans << endl;
}

int main()
{
    fastIO;
    int T = 1;
    //cin >> T;
    while (T--)
    {
        solve();
    }

    return 0;
}

/*


8 6
1 2
2 3
3 4
5 6
6 7
7 8

4 3
1 2
2 3
3 4

4 4
1 2
2 3
3 4
4 2

8 10
1 2
2 3
3 1
1 5
5 6
6 1
1 7
7 8
8 1
9 8

*/

Compilation message

count_triplets.cpp: In function 'void dfs(int, int)':
count_triplets.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for (int j = 0; j < g[v].size(); j++)
      |                     ~~^~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(int, int)':
count_triplets.cpp:129:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |         for (int i = 0; i < sizes.size(); i++)
      |                         ~~^~~~~~~~~~~~~~
count_triplets.cpp:141:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |         for (int i = 0; i < sizes.size(); i++)
      |                         ~~^~~~~~~~~~~~~~
count_triplets.cpp:147:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |         for (int i = 0; i < sizes.size(); i++)
      |                         ~~^~~~~~~~~~~~~~
count_triplets.cpp:138:12: warning: unused variable 'y' [-Wunused-variable]
  138 |         LL y = G[v].size();
      |            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 73820 KB Output is correct
2 Correct 18 ms 73820 KB Output is correct
3 Correct 18 ms 73820 KB Output is correct
4 Correct 16 ms 73688 KB Output is correct
5 Correct 17 ms 73740 KB Output is correct
6 Correct 17 ms 73820 KB Output is correct
7 Incorrect 16 ms 73816 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 73820 KB Output is correct
2 Correct 18 ms 73820 KB Output is correct
3 Correct 18 ms 73820 KB Output is correct
4 Correct 16 ms 73688 KB Output is correct
5 Correct 17 ms 73740 KB Output is correct
6 Correct 17 ms 73820 KB Output is correct
7 Incorrect 16 ms 73816 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 100196 KB Output is correct
2 Correct 113 ms 100044 KB Output is correct
3 Correct 131 ms 109392 KB Output is correct
4 Correct 136 ms 102560 KB Output is correct
5 Correct 132 ms 102076 KB Output is correct
6 Correct 152 ms 108140 KB Output is correct
7 Correct 130 ms 105312 KB Output is correct
8 Correct 152 ms 106256 KB Output is correct
9 Correct 134 ms 103060 KB Output is correct
10 Correct 131 ms 102100 KB Output is correct
11 Correct 88 ms 94292 KB Output is correct
12 Correct 91 ms 94276 KB Output is correct
13 Correct 77 ms 92736 KB Output is correct
14 Correct 89 ms 92640 KB Output is correct
15 Correct 60 ms 88912 KB Output is correct
16 Correct 62 ms 88744 KB Output is correct
17 Correct 18 ms 75820 KB Output is correct
18 Correct 18 ms 75612 KB Output is correct
19 Correct 18 ms 75820 KB Output is correct
20 Correct 20 ms 75788 KB Output is correct
21 Correct 18 ms 75864 KB Output is correct
22 Correct 19 ms 75740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 74076 KB Output is correct
2 Correct 17 ms 74076 KB Output is correct
3 Correct 22 ms 73992 KB Output is correct
4 Correct 19 ms 74332 KB Output is correct
5 Correct 19 ms 74296 KB Output is correct
6 Correct 20 ms 74072 KB Output is correct
7 Correct 18 ms 74076 KB Output is correct
8 Correct 18 ms 74076 KB Output is correct
9 Correct 17 ms 74072 KB Output is correct
10 Correct 18 ms 74012 KB Output is correct
11 Correct 19 ms 74076 KB Output is correct
12 Correct 18 ms 74152 KB Output is correct
13 Correct 21 ms 74076 KB Output is correct
14 Correct 20 ms 74072 KB Output is correct
15 Correct 19 ms 74076 KB Output is correct
16 Correct 18 ms 73820 KB Output is correct
17 Correct 20 ms 74076 KB Output is correct
18 Correct 18 ms 74076 KB Output is correct
19 Correct 18 ms 74072 KB Output is correct
20 Correct 18 ms 74008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 106700 KB Output is correct
2 Correct 159 ms 106652 KB Output is correct
3 Correct 149 ms 106860 KB Output is correct
4 Correct 152 ms 106708 KB Output is correct
5 Correct 162 ms 106856 KB Output is correct
6 Correct 198 ms 122568 KB Output is correct
7 Correct 187 ms 117124 KB Output is correct
8 Correct 209 ms 116484 KB Output is correct
9 Correct 172 ms 113388 KB Output is correct
10 Correct 139 ms 106608 KB Output is correct
11 Correct 144 ms 106436 KB Output is correct
12 Correct 144 ms 106444 KB Output is correct
13 Correct 152 ms 106496 KB Output is correct
14 Correct 124 ms 103632 KB Output is correct
15 Correct 130 ms 100704 KB Output is correct
16 Correct 66 ms 90960 KB Output is correct
17 Correct 110 ms 105156 KB Output is correct
18 Correct 136 ms 104832 KB Output is correct
19 Correct 132 ms 105264 KB Output is correct
20 Correct 110 ms 104608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 74076 KB Output is correct
2 Correct 18 ms 74076 KB Output is correct
3 Incorrect 18 ms 74032 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 106708 KB Output is correct
2 Correct 171 ms 107104 KB Output is correct
3 Incorrect 144 ms 104804 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 73820 KB Output is correct
2 Correct 18 ms 73820 KB Output is correct
3 Correct 18 ms 73820 KB Output is correct
4 Correct 16 ms 73688 KB Output is correct
5 Correct 17 ms 73740 KB Output is correct
6 Correct 17 ms 73820 KB Output is correct
7 Incorrect 16 ms 73816 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 73820 KB Output is correct
2 Correct 18 ms 73820 KB Output is correct
3 Correct 18 ms 73820 KB Output is correct
4 Correct 16 ms 73688 KB Output is correct
5 Correct 17 ms 73740 KB Output is correct
6 Correct 17 ms 73820 KB Output is correct
7 Incorrect 16 ms 73816 KB Output isn't correct
8 Halted 0 ms 0 KB -