Submission #152964

# Submission time Handle Problem Language Result Execution time Memory
152964 2019-09-10T23:39:48 Z mhy908 Duathlon (APIO18_duathlon) C++14
0 / 100
269 ms 39536 KB
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
int n, m;
pair<int, int> ed[200010];
vector<int> input[100010], link[100010], linkrev[100010], bcc[100010];
bool ch[100010];
int arr[100010], re, c, col[100010];
LL subtree[100010], dp[100010][5], minu[100010];
LL ans;
unordered_set<LL> us;
void give_dir(int num)
{
    if(ch[num])return;
    ch[num]=true;
    for(int i=0; i<input[num].size(); i++){
        if(!us.count((LL)num*200000+(LL)input[num][i])&&!us.count((LL)input[num][i]*200000+(LL)num)){
            link[num].pb(input[num][i]);
            linkrev[input[num][i]].pb(num);
            us.insert((LL)num*200000+(LL)input[num][i]);
            us.insert((LL)input[num][i]*200000+(LL)num);
            //printf("%d -> %d\n", num, input[num][i]);
        }
        give_dir(input[num][i]);
    }
}
void dfs1(int num)
{
    if(ch[num])return;
    ch[num]=true;
    for(int i=0; i<link[num].size(); i++){
        dfs1(link[num][i]);
    }
    arr[++re]=num;
}
void dfs2(int num, int color)
{
    if(col[num])return;
    col[num]=color;
    for(int i=0; i<linkrev[num].size(); i++){
        dfs2(linkrev[num][i], color);
    }
}
void tree_dp(int num)
{
    if(ch[num])return;
    ch[num]=true;
    dp[num][1]=subtree[num];
    dp[num][2]=(subtree[num]-1)*(subtree[num]-1);
    dp[num][3]=(subtree[num])*(subtree[num]-1)*(subtree[num]-2);
    minu[num]=subtree[num]-1;
    for(int i=0; i<bcc[num].size(); i++){
        if(ch[bcc[num][i]])continue;
        tree_dp(bcc[num][i]);
        dp[num][1]+=dp[bcc[num][i]][1];
        dp[num][2]+=dp[bcc[num][i]][2];
        dp[num][3]+=dp[bcc[num][i]][3];
        minu[num]+=minu[bcc[num][i]];
        if(subtree[num]==1)minu[num]+=dp[bcc[num][i]][1];
        else minu[num]+=2*dp[bcc[num][i]][1];
        dp[num][2]+=subtree[num]*dp[bcc[num][i]][1];
        dp[num][3]+=(subtree[num]-1)*(subtree[num]-1)*dp[bcc[num][i]][1]*2;
        dp[num][3]-=2*subtree[num]*minu[bcc[num][i]];
    }
    dp[num][3]+=subtree[num]*(dp[num][1]-subtree[num])*(dp[num][1]-subtree[num]-1)*2;
    //fucking shit.....

}
int main()
{
    scanf("%d %d", &n, &m);
    for(int i=1; i<=m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        input[a].pb(b);
        input[b].pb(a);
        ed[i].F=a;
        ed[i].S=b;
    }
    for(int i=1; i<=n; i++){
        give_dir(i);
    }
    memset(ch, 0, sizeof(ch));
    for(int i=1; i<=n; i++){
        if(!ch[i])dfs1(i);
    }
    for(int i=n; i>=1; i--){
        if(!col[arr[i]])dfs2(arr[i], ++c);
        subtree[col[arr[i]]]++;
    }
    for(int i=1; i<=m; i++){
        if(col[ed[i].F]!=col[ed[i].S]){
            bcc[col[ed[i].F]].pb(col[ed[i].S]);
            bcc[col[ed[i].S]].pb(col[ed[i].F]);
        }
    }
    for(int i=1; i<=c; i++){
        sort(all(bcc[i]));
        bcc[i].erase(unique(all(bcc[i])), bcc[i].end());
    }
    memset(ch, 0, sizeof(ch));
    for(int i=1; i<=c; i++){
        if(!ch[i]){
            tree_dp(i);
            ans+=dp[i][3];
        }
    }
    printf("%lld", ans);
}

Compilation message

count_triplets.cpp: In function 'void give_dir(int)':
count_triplets.cpp:20:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<input[num].size(); i++){
                  ~^~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs1(int)':
count_triplets.cpp:35:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<link[num].size(); i++){
                  ~^~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(int, int)':
count_triplets.cpp:44:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<linkrev[num].size(); i++){
                  ~^~~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void tree_dp(int)':
count_triplets.cpp:56:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<bcc[num].size(); i++){
                  ~^~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 11 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 10 ms 9820 KB Output is correct
6 Correct 10 ms 9848 KB Output is correct
7 Incorrect 10 ms 9768 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 11 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 10 ms 9820 KB Output is correct
6 Correct 10 ms 9848 KB Output is correct
7 Incorrect 10 ms 9768 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 262 ms 38516 KB Output is correct
2 Correct 241 ms 39536 KB Output is correct
3 Incorrect 269 ms 39056 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 10104 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 257 ms 36368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 10108 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 247 ms 36216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 11 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 10 ms 9820 KB Output is correct
6 Correct 10 ms 9848 KB Output is correct
7 Incorrect 10 ms 9768 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 11 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 10 ms 9820 KB Output is correct
6 Correct 10 ms 9848 KB Output is correct
7 Incorrect 10 ms 9768 KB Output isn't correct
8 Halted 0 ms 0 KB -