# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
710345 |
2023-03-15T07:33:13 Z |
lam |
Duathlon (APIO18_duathlon) |
C++14 |
|
120 ms |
41120 KB |
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 10;
int n,m;
vector <int> adj[maxn],adj2[maxn];
int dp[maxn],dp2[maxn];
stack <int> st;
int l[maxn],t[maxn],cnt2;
int scc, id[maxn], s[maxn];
bool dau[maxn];
vector <int> root;
int ans = 0LL;
void dfs(int x, int p)
{
dau[x] = 1;
dp[x] = 0;
for (int i:adj[x])
if (i!=p)
{
dfs(i,x);
dp[x] += dp[i]+s[i];
}
}
void dfs2(int x, int p, int val)
{
dp2[x] = val;
val+=s[x];
vector <int> pre,suf;
for (int i:adj[x])
if (i!=p)
{
pre.push_back(dp[i]+s[i]);
suf.push_back(dp[i]+s[i]);
}
for (int i=1; i<pre.size(); i++) pre[i]+=pre[i-1];
for (int i=suf.size()-2; i>=0; i--) suf[i]+=suf[i+1];
int cnt=0;
for (int i:adj[x])
if (i!=p)
{
int val2 = val;
if (cnt>0) val2 += pre[cnt-1];
if (cnt<(int)suf.size()-1) val2+=suf[cnt+1];
cnt++;
dfs2(i,x,val2);
}
}
void dfs3(int x, int p, int sz)
{
int val = dp2[x];
int temp = (sz - s[x] - val);
if (temp>0&&val>0)
ans += val*temp*s[x];
for (int i:adj[x])
if (i!=p)
{
dfs3(i,x,sz);
val = dp[i]+s[i];
temp = sz - s[x] - val;
if (temp>0&&val>0) ans+=val*temp*s[x];
}
}
void build(int x, int p)
{
st.push(x);
l[x]=t[x]=++cnt2;
for (int i:adj2[x])
if (i!=p)
{
if (t[i]==0)
{
build(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);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>n>>m;
for (int i=1; i<=m; i++)
{
int u,v; cin>>u>>v;
adj2[u].push_back(v);
adj2[v].push_back(u);
}
scc=cnt2=0;
for (int i=1; i<=n; i++) if (!t[i]) build(i,i);
for (int i=1; i<=n; i++)
for (int j:adj2[i])
if (id[i]!=id[j])
adj[id[i]].push_back(id[j]);
n = scc;
fill_n(dau,n+1,0);
for (int i=1; i<=n; i++) if (!dau[i]) dfs(i,i), root.push_back(i);
for (int i:root) dfs2(i,i,0);
for (int i=1; i<=n; i++) if (s[i]>=3) ans += s[i]*(s[i]-1)*(s[i]-2);
for (int i:root) dfs3(i,i,dp[i]+s[i]);
cout<<ans<<'\n';
}
Compilation message
count_triplets.cpp: In function 'void dfs2(long long int, long long int, long long int)':
count_triplets.cpp:37: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]
37 | for (int i=1; i<pre.size(); i++) pre[i]+=pre[i-1];
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4996 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 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 |
4 ms |
4996 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 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 |
17544 KB |
Output is correct |
2 |
Correct |
58 ms |
17484 KB |
Output is correct |
3 |
Correct |
106 ms |
25780 KB |
Output is correct |
4 |
Correct |
64 ms |
18252 KB |
Output is correct |
5 |
Correct |
63 ms |
20680 KB |
Output is correct |
6 |
Correct |
88 ms |
27412 KB |
Output is correct |
7 |
Correct |
91 ms |
18756 KB |
Output is correct |
8 |
Correct |
97 ms |
22824 KB |
Output is correct |
9 |
Correct |
81 ms |
16416 KB |
Output is correct |
10 |
Correct |
65 ms |
17524 KB |
Output is correct |
11 |
Correct |
54 ms |
14000 KB |
Output is correct |
12 |
Correct |
54 ms |
13900 KB |
Output is correct |
13 |
Correct |
68 ms |
14264 KB |
Output is correct |
14 |
Correct |
55 ms |
14020 KB |
Output is correct |
15 |
Correct |
46 ms |
14320 KB |
Output is correct |
16 |
Correct |
55 ms |
13932 KB |
Output is correct |
17 |
Correct |
9 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 |
10 ms |
10704 KB |
Output is correct |
22 |
Correct |
11 ms |
10704 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 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5332 KB |
Output is correct |
5 |
Correct |
5 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5420 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 |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
5 ms |
5076 KB |
Output is correct |
17 |
Correct |
4 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
17280 KB |
Output is correct |
2 |
Correct |
82 ms |
17228 KB |
Output is correct |
3 |
Correct |
89 ms |
17196 KB |
Output is correct |
4 |
Correct |
120 ms |
17196 KB |
Output is correct |
5 |
Correct |
80 ms |
17228 KB |
Output is correct |
6 |
Correct |
117 ms |
41120 KB |
Output is correct |
7 |
Correct |
110 ms |
25852 KB |
Output is correct |
8 |
Correct |
96 ms |
23652 KB |
Output is correct |
9 |
Correct |
96 ms |
21532 KB |
Output is correct |
10 |
Correct |
94 ms |
17264 KB |
Output is correct |
11 |
Correct |
101 ms |
17312 KB |
Output is correct |
12 |
Correct |
108 ms |
17224 KB |
Output is correct |
13 |
Correct |
95 ms |
17248 KB |
Output is correct |
14 |
Correct |
89 ms |
17008 KB |
Output is correct |
15 |
Correct |
73 ms |
16632 KB |
Output is correct |
16 |
Correct |
44 ms |
15140 KB |
Output is correct |
17 |
Correct |
55 ms |
19244 KB |
Output is correct |
18 |
Correct |
61 ms |
19740 KB |
Output is correct |
19 |
Correct |
65 ms |
20160 KB |
Output is correct |
20 |
Correct |
58 ms |
19168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 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 |
86 ms |
17272 KB |
Output is correct |
2 |
Correct |
88 ms |
17020 KB |
Output is correct |
3 |
Incorrect |
98 ms |
15156 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4996 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 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 |
4 ms |
4996 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |