Submission #153011

# Submission time Handle Problem Language Result Execution time Memory
153011 2019-09-11T11:21:47 Z mhy908 Duathlon (APIO18_duathlon) C++14
31 / 100
366 ms 39908 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];
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(dp[num])return;
    dp[num]=subtree[num];
    ans+=subtree[num]*(subtree[num]-1)*(subtree[num]-2);
    for(int i=0; i<bcc[num].size(); i++){
        if(dp[bcc[num][i]])continue;
        tree_dp(bcc[num][i]);
        dp[num]+=dp[bcc[num][i]];
    }
}
void BCC_dfs(int num)
{
    if(ch[num])return;
    ch[num]=true;
    LL temp=0;
    for(int i=0; i<bcc[num].size(); i++){
        ans+=(subtree[num]-1)*(subtree[num]-1)*dp[bcc[num][i]]*2;
        temp+=dp[bcc[num][i]];
        ans-=subtree[num]*dp[bcc[num][i]]*dp[bcc[num][i]];
    }
    ans+=subtree[num]*temp*temp;
    for(int i=0; i<bcc[num].size(); i++){
        LL tnum=dp[num], tbcc=dp[bcc[num][i]];
        dp[num]=temp-tbcc+subtree[num];
        dp[bcc[num][i]]=tnum;
        BCC_dfs(bcc[num][i]);
        dp[bcc[num][i]]=tbcc;
        dp[num]=tnum;
    }
}
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++)tree_dp(i);
    for(int i=1; i<=c; i++)BCC_dfs(i);
    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:53: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 'void BCC_dfs(int)':
count_triplets.cpp:64:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<bcc[num].size(); i++){
                  ~^~~~~~~~~~~~~~~~
count_triplets.cpp:70: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:81: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:84: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 10 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 11 ms 9848 KB Output is correct
6 Correct 11 ms 9976 KB Output is correct
7 Incorrect 11 ms 9848 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 10 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 11 ms 9848 KB Output is correct
6 Correct 11 ms 9976 KB Output is correct
7 Incorrect 11 ms 9848 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 303 ms 38220 KB Output is correct
2 Correct 301 ms 38172 KB Output is correct
3 Correct 363 ms 35768 KB Output is correct
4 Correct 317 ms 38276 KB Output is correct
5 Correct 286 ms 34744 KB Output is correct
6 Correct 307 ms 35772 KB Output is correct
7 Correct 272 ms 34744 KB Output is correct
8 Correct 299 ms 35640 KB Output is correct
9 Correct 292 ms 33720 KB Output is correct
10 Correct 314 ms 34176 KB Output is correct
11 Correct 241 ms 30264 KB Output is correct
12 Correct 234 ms 29880 KB Output is correct
13 Correct 218 ms 29244 KB Output is correct
14 Correct 214 ms 28844 KB Output is correct
15 Correct 165 ms 26296 KB Output is correct
16 Correct 170 ms 26000 KB Output is correct
17 Correct 17 ms 12280 KB Output is correct
18 Correct 17 ms 12280 KB Output is correct
19 Correct 17 ms 12280 KB Output is correct
20 Correct 17 ms 12152 KB Output is correct
21 Correct 17 ms 12152 KB Output is correct
22 Correct 17 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 10108 KB Output is correct
2 Correct 13 ms 10104 KB Output is correct
3 Correct 12 ms 10104 KB Output is correct
4 Correct 12 ms 10232 KB Output is correct
5 Correct 13 ms 10108 KB Output is correct
6 Correct 13 ms 10076 KB Output is correct
7 Correct 12 ms 10104 KB Output is correct
8 Correct 15 ms 10104 KB Output is correct
9 Correct 13 ms 10104 KB Output is correct
10 Correct 12 ms 10104 KB Output is correct
11 Correct 12 ms 10104 KB Output is correct
12 Correct 12 ms 10104 KB Output is correct
13 Correct 12 ms 10104 KB Output is correct
14 Correct 11 ms 10104 KB Output is correct
15 Correct 12 ms 10104 KB Output is correct
16 Correct 11 ms 9980 KB Output is correct
17 Correct 11 ms 10104 KB Output is correct
18 Correct 12 ms 10108 KB Output is correct
19 Correct 12 ms 10104 KB Output is correct
20 Correct 12 ms 10104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 281 ms 32292 KB Output is correct
2 Correct 276 ms 33588 KB Output is correct
3 Correct 276 ms 33468 KB Output is correct
4 Correct 366 ms 33468 KB Output is correct
5 Correct 327 ms 33496 KB Output is correct
6 Correct 318 ms 39908 KB Output is correct
7 Correct 309 ms 37304 KB Output is correct
8 Correct 296 ms 36280 KB Output is correct
9 Correct 303 ms 35388 KB Output is correct
10 Correct 285 ms 33468 KB Output is correct
11 Correct 302 ms 33596 KB Output is correct
12 Correct 297 ms 33484 KB Output is correct
13 Correct 275 ms 33504 KB Output is correct
14 Correct 292 ms 32188 KB Output is correct
15 Correct 234 ms 30648 KB Output is correct
16 Correct 200 ms 24684 KB Output is correct
17 Correct 247 ms 32768 KB Output is correct
18 Correct 229 ms 32756 KB Output is correct
19 Correct 261 ms 33200 KB Output is correct
20 Correct 243 ms 32828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 10104 KB Output is correct
2 Correct 15 ms 10104 KB Output is correct
3 Incorrect 15 ms 10076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 297 ms 32100 KB Output is correct
2 Correct 295 ms 33696 KB Output is correct
3 Incorrect 352 ms 35848 KB Output isn't correct
4 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 10 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 11 ms 9848 KB Output is correct
6 Correct 11 ms 9976 KB Output is correct
7 Incorrect 11 ms 9848 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 10 ms 9848 KB Output is correct
4 Correct 10 ms 9848 KB Output is correct
5 Correct 11 ms 9848 KB Output is correct
6 Correct 11 ms 9976 KB Output is correct
7 Incorrect 11 ms 9848 KB Output isn't correct
8 Halted 0 ms 0 KB -