답안 #710281

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710281 2023-03-15T06:38:16 Z lam 철인 이종 경기 (APIO18_duathlon) C++14
0 / 100
147 ms 35420 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;
    for (int i:adj2[x])
        if (i!=p)
    {
        dfs2(i,x);
        int temp = n-dp[i]-s[i] - s[x];
//        cout<<temp<<" : "<<dp[i]+s[i]<<' '<<s[x]<<endl;
        ans += s[x]*(dp[i]+s[i])*temp;
        dp[x] = (dp[x] + dp[i] + s[i]) ;
    }
}

void dfs3(int x, int p, int val)
{
    int temp = (n - val - s[x]);
    ans += s[x]*val*temp;
//    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);
        }
}
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), root.push_back(id[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:root) dfs2(i,i);
    for (int i:root) dfs3(i,i,0);
//    cout<<ans<<endl;
    for (int i=1; i<=scc; i++)
    {
        assert(s[i]==1);
        int sl = s[i];
//        cout<<i<<" = "<<s[i]<<' '<<dp[i]<<' '<<dp2[i]<<endl;
        int temp = 0;
        assert(sl!=0);
        if (sl>=3)
        {
            temp = sl*(sl-1)*(sl-2);
            ans = (ans + temp);
        }
    }
    cout<<ans<<'\n';
}

Compilation message

count_triplets.cpp: In function 'void dfs3(long long int, long long int, long long int)':
count_triplets.cpp:71: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]
   71 |     for (int i=1; i<pre.size(); i++) pre[i] = (pre[i-1] + pre[i]) ;
      |                   ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 10068 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 10068 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 90 ms 35420 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 4 ms 5200 KB Output is correct
3 Correct 6 ms 5172 KB Output is correct
4 Correct 4 ms 5332 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 5 ms 5204 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 7 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Incorrect 5 ms 5076 KB Output isn't correct
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 16416 KB Output is correct
2 Correct 91 ms 16684 KB Output is correct
3 Correct 86 ms 16592 KB Output is correct
4 Correct 99 ms 16700 KB Output is correct
5 Correct 109 ms 16604 KB Output is correct
6 Correct 94 ms 29044 KB Output is correct
7 Correct 132 ms 25296 KB Output is correct
8 Correct 147 ms 23168 KB Output is correct
9 Correct 113 ms 20948 KB Output is correct
10 Incorrect 142 ms 16644 KB Output isn't correct
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 4 ms 5080 KB Output is correct
3 Runtime error 12 ms 10196 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 16424 KB Output is correct
2 Correct 134 ms 16364 KB Output is correct
3 Runtime error 120 ms 29692 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 10068 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 10068 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -