이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
int N,M,X[200200],Y[200200],P[200200]; bool chk[300300];
vector<int> G[100100],V[300300];
int dep[200200],low[200200];
int sz[300300],ho[300300], siz[300300];
int find(int x)
{
if (P[x] != x) P[x] = find(P[x]);
return P[x];
}
void go(int x, int l)
{
low[x] = dep[x];
chk[x] = 1;
for (auto &i : G[x]) if (i != l){
int y = (x == X[i] ? Y[i] : X[i]);
if (!chk[y]){
dep[y] = dep[x] + 1;
go(y,i);
if (dep[x] > low[y]){
if (l != -1) P[find(l)] = find(i);
}
if (low[x] > low[y])
low[x] = low[y];
}
else if (dep[x] > dep[y]){
if (l != -1) P[find(l)] = find(i);
if (low[x] > dep[y]){
low[x] = dep[y];
}
}
}
}
vector<int> comp;
void gather(int x, int l)
{
comp.push_back(x);
chk[x] = 1;
siz[x] = sz[x];
for (auto &y : V[x]) if (y != l){
gather(y,x);
siz[x] += siz[y];
}
}
long long ans;
void bye(int v, int p, int c){
ans += ll(sz[v]) * (ll(sz[v]) - 1ll) * (ll(sz[v]) - 2ll) / 2ll; // first
if (sz[v])
ans += (c - ll(sz[v])) * (ll(sz[v]) - 1ll) * (ll(sz[v]));
ll sum = 0ll;
if (v >= N)
ans += (ll(V[v].size()) * (ll(V[v].size()) - 1ll));
for (int u : V[v]){
if (v < N)
ans += (ll(sz[u]) * (ll(sz[u]) - 1ll));
if (u != p) {
bye(u, v, c);
ans += sum * ll(siz[u]) * ll(sz[v]);
sum += ll(siz[u]);
} else {
ans += sum * (c - ll(siz[v])) * ll(sz[v]);
sum += (c - ll(siz[v]));
}
ans += ll(sz[v]) * (ll(sz[v]) - 1) * ll(sz[u]) / 2ll; //new
}
}
int main(){
// freopen("in.txt","r",stdin);
scanf ("%d %d",&N,&M);
for (int i=0;i<M;i++){
scanf ("%d %d",&X[i],&Y[i]);
X[i]--; Y[i]--;
G[X[i]].push_back(i);
G[Y[i]].push_back(i);
P[i] = i;
}
for (int i=0;i<N;i++) if (!chk[i]) go(i,-1);
for (int i=0;i<N;i++){
set<int> s;
for (auto &e : G[i]){
s.insert(find(e));
}
if (s.size() == 1) sz[N+*s.begin()]++;
else{
sz[i] = 1;
for (auto &e : s){
V[i].push_back(N+e);
V[N+e].push_back(i);
}
}
}
for (int i=0;i<N+M;i++) chk[i] = 0;
for (int i=0;i<N+M;i++) if (!chk[i]){
comp.clear();
gather(i,-1);
int C = 0;
for (auto &x : comp) C += sz[x];
for (auto &x : comp){
ho[x] = sz[x];
for (auto &y : V[x]) ho[x] += sz[y];
}
// ans += (long long) C * (C - 1) * (C - 2);
bye(i,-1,C);
}
printf ("%lld\n",ans * 2);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:89:8: 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:91:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d",&X[i],&Y[i]);
~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |