이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int nodes, N, M, lev[100009], dp[100009], vol[200009], papa[200009], compSize[200009];
long long sumProd[200009];
stack < pair < int, int > > stk;
vector < int > v[100009], h[200009];
void addComp (pair < int, int > whenToStop)
{
pair < int, int > curr;
vector < int > currComp;
do {
curr = stk.top ();
stk.pop ();
currComp.push_back (curr.first);
currComp.push_back (curr.second);
}while (curr != whenToStop);
sort (currComp.begin (), currComp.end ());
currComp.erase (unique (currComp.begin (), currComp.end ()), currComp.end ());
/* for (auto it : currComp)
printf ("%d ", it);
printf ("\n");*/
nodes ++;
for (auto it : currComp)
h[it].push_back (nodes),
h[nodes].push_back (it);
}
void dfs (int nod, int tata)
{
dp[nod] = lev[nod];
for (auto it : v[nod])
if (lev[it] == 0)
{
lev[it] = lev[nod] + 1;
stk.push ({it, nod});
dfs (it, nod);
if (dp[it] < dp[nod])
dp[nod] = dp[it];
if (dp[it] >= lev[nod])
addComp ({it, nod});
}
for (auto it : v[nod])
if (it != tata)
{
if (lev[it] < dp[nod])
dp[nod] = lev[it];
}
}
vector < int > currComp;
void init (int nod, int tata)
{
currComp.push_back (nod);
vol[nod] = (nod <= N), papa[nod] = tata;
int sum = 0;
for (auto it : h[nod])
if (it != tata)
init (it, nod), vol[nod] += vol[it],
sumProd[nod] += 1LL * sum * vol[it],
sum += vol[it];
}
namespace treeSubtask {
int vol[100009];
void dfs (int nod, int tata)
{
vol[nod] = 1;
for (auto it : v[nod])
if (it != tata)
dfs (it, nod), vol[nod] += vol[it];
}
long long solveTree ()
{
dfs (1, -1);
long long ans = 0;
for (int x=1; x<=N; x++)
{
int sum = 0;
for (auto it : v[x])
{
int currSize = 0;
if (vol[it] > vol[x]) currSize = N - vol[x];
else currSize = vol[it];
ans += 1LL * sum * currSize;
sum += currSize;
}
}
return ans * 2LL;
}
};
void buildBiconnectedTree ()
{
nodes = N;
for (int i=1; i<=N; i++)
if (lev[i] == 0)
lev[i] = 1, dfs (i, -1);
}
void read ()
{
scanf ("%d %d", &N, &M);
while (M --)
{
int x, y;
scanf ("%d %d", &x, &y);
v[x].push_back (y);
v[y].push_back (x);
}
}
void precalc ()
{
for (int i=1; i<=N; i++)
if (vol[i] == 0)
{
currComp.clear (), init (i, -1);
for (auto nod : currComp)
sumProd[nod] += 1LL * (vol[nod] - (nod <= N)) * (vol[i] - vol[nod]),
sumProd[nod] <<= 1LL, compSize[nod] = vol[i];
}
}
int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);
read ();
buildBiconnectedTree ();
precalc ();
long long ans = 0;
for (int x=1; x<=N; x++)
{
long long before = ans;
ans += sumProd[x];
for (auto comp : h[x])
{
ans += sumProd[comp];
if (papa[comp] == x)
ans -= 2LL * (compSize[x] - vol[comp]) * vol[comp];
else
ans -= 2LL * (compSize[x] - vol[x]) * vol[x];
}
}
printf ("%lld\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:138:15: warning: unused variable 'before' [-Wunused-variable]
long long before = ans;
^~~~~~
count_triplets.cpp: In function 'void read()':
count_triplets.cpp:105:11: 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:109:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d", &x, &y);
~~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |