Submission #710311

# Submission time Handle Problem Language Result Execution time Memory
710311 2023-03-15T07:00:53 Z lam Duathlon (APIO18_duathlon) C++14
31 / 100
153 ms 41116 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int n,m;
vector <int> adj[maxn];
stack <int> st;
int l[maxn],t[maxn],cnt,scc,id[maxn];
int dp[maxn],dp2[maxn],s[maxn];
vector <int> adj2[maxn];
int ans = 0LL;
bool dau[maxn];
void dfs(int x, int p)
{
    st.push(x);
    l[x] = t[x] = ++cnt;
    for (int i:adj[x])
        if (i!=p)
    {
        if (!t[i])
        {
            dfs(i,x);
            l[x]=min(l[x],l[i]);
        }
        else l[x]=min(l[x],t[i]);
    }
    if (l[x]==t[x])
    {
        scc++; int temp;
        do
        {
            temp = st.top(); st.pop();
            s[scc]++;
            id[temp] = scc;
        }
        while (temp!=x);
    }
}

void dfs2(int x, int p)
{
    dp[x] = 0;
    dau[x] = 1;
    for (int i:adj2[x])
        if (i!=p)
    {
        dfs2(i,x);
//        cout<<temp<<" : "<<dp[i]+s[i]<<' '<<s[x]<<endl;
        dp[x] = (dp[x] + dp[i] + s[i]) ;
    }
}

void dfs3(int x, int p, int val)
{
    dp2[x] = val;
//    cout<<val<<" : "<<temp<<" : "<<s[x]<<endl;
    vector <int> pre,suf;
    for (int i:adj2[x])
        if (i!=p)
    {
        pre.push_back((dp[i]+s[i]) );
        suf.push_back((dp[i]+s[i]) );
    }
    val = (val + s[x]) ;
//    cout<<x<<" :: "<<val<<endl;
//    for (int i:pre) cout<<i<<' '; cout<<"@@@@@@@@@@@"<<endl;
//    for (int i:suf) cout<<i<<' '; cout<<"@@@@@@@@@@@"<<endl;
    for (int i=1; i<pre.size(); i++) pre[i] = (pre[i-1] + pre[i]) ;
    for (int i=suf.size()-2; i>=0; i--) suf[i] = (suf[i+1] + suf[i]) ;
//    cout<<(int)suf.size()<<"@%$@%#$@"<<endl;
    int cnt2=0;
    for (int i:adj2[x])
        if (i!=p)
        {
            int val2 = val;
//            cerr<<val2<<' '<<cnt2<<" -> ";
            if (cnt2>0) val2 = (val2 + pre[cnt2-1]) ;
            if (cnt2<=(int)suf.size()-2) val2 = (val2 + suf[cnt2+1]) ;
//             cerr<<val<<endl;
            cnt2++;
            dfs3(i,x,val2);
        }
}
void dfs4(int x, int p, int sz)
{
    int val = dp2[x];
    int temp = (sz - s[x] - val);
    if (temp>0&&val>0)
    ans += s[x]*val*temp;
    for (int i:adj2[x])
        if (i!=p)
    {
        dfs4(i,x,sz);
        val = dp[i]+s[i];
        temp = sz - s[x] - val;
        if (temp>0&&val>0) ans+=s[x]*val*temp;
    }
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
//    freopen("dualthon.inp","r",stdin);
//    freopen("dualthon.ans","w",stdout);
    cin>>n>>m;
    for (int i=1; i<=m; i++)
    {
        int u,v; cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    cnt = scc= 0;
    vector <int> root;
    for (int i=1; i<=n; i++) if (!t[i]) dfs(i,i);
    for (int i=1; i<=n; i++)
        for (int j:adj[i])
            if (id[i]!=id[j])
    {
        adj2[id[i]].push_back(id[j]);
    }
    for (int i=1; i<=scc; i++)
    {
        sort(adj2[i].begin(),adj2[i].end());
        adj2[i].resize(unique(adj2[i].begin(),adj2[i].end())-adj2[i].begin());
    }
    for (int i=1; i<=scc; i++) if (!dau[i]) dfs2(i,i), root.push_back(i);
    for (int i:root) dfs3(i,i,0);
    for (int i=1; i<=scc; i++)
    {
        int sl = s[i];
//        cout<<i<<" := "<<dp[i]<<' '<<dp2[i]<<endl;
        int temp = 0;
        if (sl>=3)
        {
            temp = sl*(sl-1)*(sl-2);
            ans = (ans + temp);
        }
        if (sl>=2)
        {
            temp = (sl-1)*(sl-1);
//            cout<<temp<<" "<<dp[i]<<' '<<dp2[i]<<endl;
            ans += temp*dp[i]*2;
            ans += temp*dp2[i]*2;
        }
    }
    for (int i:root) dfs4(i,i,dp[i]+s[i]);
    cout<<ans<<'\n';
}

Compilation message

count_triplets.cpp: In function 'void dfs3(long long int, long long int, long long int)':
count_triplets.cpp:69:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for (int i=1; i<pre.size(); i++) pre[i] = (pre[i-1] + pre[i]) ;
      |                   ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Incorrect 2 ms 4948 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Incorrect 2 ms 4948 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 61 ms 17564 KB Output is correct
2 Correct 48 ms 17484 KB Output is correct
3 Correct 86 ms 25796 KB Output is correct
4 Correct 62 ms 18176 KB Output is correct
5 Correct 70 ms 20676 KB Output is correct
6 Correct 76 ms 27384 KB Output is correct
7 Correct 75 ms 18768 KB Output is correct
8 Correct 71 ms 22820 KB Output is correct
9 Correct 88 ms 16372 KB Output is correct
10 Correct 92 ms 17496 KB Output is correct
11 Correct 76 ms 13896 KB Output is correct
12 Correct 61 ms 13896 KB Output is correct
13 Correct 57 ms 14228 KB Output is correct
14 Correct 63 ms 14024 KB Output is correct
15 Correct 47 ms 14308 KB Output is correct
16 Correct 47 ms 14036 KB Output is correct
17 Correct 8 ms 10704 KB Output is correct
18 Correct 9 ms 10704 KB Output is correct
19 Correct 9 ms 10700 KB Output is correct
20 Correct 8 ms 10700 KB Output is correct
21 Correct 9 ms 10704 KB Output is correct
22 Correct 8 ms 10712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 4 ms 5332 KB Output is correct
5 Correct 5 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5332 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 4 ms 5076 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 3 ms 5204 KB Output is correct
19 Correct 4 ms 5204 KB Output is correct
20 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 17248 KB Output is correct
2 Correct 106 ms 17236 KB Output is correct
3 Correct 103 ms 17216 KB Output is correct
4 Correct 104 ms 17204 KB Output is correct
5 Correct 130 ms 17288 KB Output is correct
6 Correct 143 ms 41116 KB Output is correct
7 Correct 153 ms 25960 KB Output is correct
8 Correct 101 ms 23712 KB Output is correct
9 Correct 106 ms 21548 KB Output is correct
10 Correct 79 ms 17228 KB Output is correct
11 Correct 81 ms 17248 KB Output is correct
12 Correct 77 ms 17224 KB Output is correct
13 Correct 104 ms 17296 KB Output is correct
14 Correct 100 ms 17020 KB Output is correct
15 Correct 68 ms 16652 KB Output is correct
16 Correct 43 ms 15044 KB Output is correct
17 Correct 59 ms 19304 KB Output is correct
18 Correct 55 ms 19752 KB Output is correct
19 Correct 57 ms 20144 KB Output is correct
20 Correct 55 ms 19172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Incorrect 3 ms 5076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 84 ms 17220 KB Output is correct
2 Correct 85 ms 16976 KB Output is correct
3 Incorrect 88 ms 15216 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Incorrect 2 ms 4948 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Incorrect 2 ms 4948 KB Output isn't correct
8 Halted 0 ms 0 KB -