제출 #61415

#제출 시각아이디문제언어결과실행 시간메모리
61415kingpig9철인 이종 경기 (APIO18_duathlon)C++11
66 / 100
338 ms52700 KiB
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5 + 10;
 
//#define debug(...) fprintf(stderr, __VA_ARGS__)
#define debug(...)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
 
ll sqr (ll x) {
	return x * x;
}
 
struct union_find {
	int par[MAXN];
	int sz[MAXN];
	union_find() {
		for (int i = 0; i < MAXN; i++) {
			par[i] = i;
			sz[i] = 1;
		}
	}
 
	int find (int x) {
		return x == par[x] ? x : par[x] = find(par[x]);
	}
 
	void merge (int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) {
			return;
		}
		par[x] = y;
		sz[y] += sz[x];
	}
};
 
int N, M;
vector<int> adj[MAXN];
vector<int> verts;
int par[MAXN], depth[MAXN];
bool vis[MAXN];
union_find uf = union_find();
union_find cy = union_find();	//cycle components
 
void dfs (int x, int p) {
	debug("x = %d\n", x);
	verts.push_back(x);
	vis[x] = true;
	par[x] = p;
 
	for (int y : adj[x]) {
		if (y != p) {
			if (vis[y]) {
				if (depth[y] < depth[x]) {
					//then it's a component
					for (int i = x; ; i = par[i]) {
						cy.merge(x, i);
						debug("merge(%d, %d)\n", x, i);
						if (i == y) {
							break;
						}
					}
				}
			} else {
				depth[y] = depth[x] + 1;
				dfs(y, x);
			}
		}
	}
}
 
vector<int> cadj[MAXN];	//comp adjacent
int cpar[MAXN];
int sub[MAXN];
vector<int> cverts;
vector<int> cyv[MAXN];
 
void dfsc (int x) {
	cverts.push_back(x);
	sub[x] = cy.sz[x];
	for (int y : cadj[x]) {
		cpar[y] = x;
		cadj[y].erase(find(all(cadj[y]), x));
		dfsc(y);
		sub[x] += sub[y];
	}
}
 
ll go (int root) {
	dfs(root, 0);
	for (int x : verts) {
		int fx = cy.find(x);
		cyv[fx].push_back(x);
		for (int y : adj[x]) {
			int fy = cy.find(y);
			if (fx != fy) {
				cadj[fx].push_back(fy);
				debug("CADJ %d %d\n", fx, fy);
			}
		}
	}
 
	int szroot = uf.sz[root];
	int froot = cy.find(root);
	dfsc(froot);
 
	//ok let's consider when the stuff is good!
	ll ans = 0;
 
	debug("WE HAVE szroot = %d\n", szroot);
	for (int c : cverts) {
		debug("--------COMPONENT OF %d----------------\n", c);
		//all 3 are in the same component
		ll s = cy.sz[c];
		ans += s * (s - 1) * (s - 2);
		debug("s = %lld\n", s);
 
		//all 3 are in different component. c is the transition.
		//also have to count individual ones
		ll pans = ans;
		ll totsb = 0;
		ll ncho2 = 0;
 		for (int x : cyv[c]) {
			ll sumsb = 0;
			for (int y : adj[x]) {
				int fy = cy.find(y);
				if (c != fy) {
					ll sb;
					if (fy == cpar[c]) {
						sb = szroot - sub[c];
					} else {
						sb = sub[fy];
					}
					ans -= sqr(sb);
					sumsb += sb;
				}
			}
			ans += sqr(sumsb);
			ncho2 -= sqr(sumsb);
			totsb += sumsb;
		}

		assert(totsb == szroot - s);
		ncho2 += sqr(totsb);
		ans += ncho2 * s;
		debug("all3indiff = %lld\n", ans - pans);
 
		//the stuff right below this comment is actually wrong.
		/*
		//two of them are in this component. one of them is in different component
		debug("s = %lld. szroot = %d. So we add %lld\n", s, szroot, 2 * s * (s - 1) * (szroot - s));
		ans += 2 * s * (s - 1) * (szroot - s);	//ans += 4 * C(s, 2) * (szroot - s)
		*/
 
		//ok now this may be correct...
		ans += 2 * s * (s - 1) * (szroot - s);	//ans += 4 * C(s, 2) * (szroot - s)
		ans -= 2 * (s - 1) * (szroot - s);
	}
 
#warning RESET AT THE END!
	verts.clear();
	cverts.clear();
	return ans;
}
 
int main() {
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= M; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
		uf.merge(x, y);
	}
 
	ll ans = 0;
	for (int i = 1; i <= N; i++) {
		if (i == uf.find(i)) {
			ans += go(i);
		}
	}
	printf("%lld\n", ans);
}

컴파일 시 표준 에러 (stderr) 메시지

count_triplets.cpp:168:2: warning: #warning RESET AT THE END! [-Wcpp]
 #warning RESET AT THE END!
  ^~~~~~~
count_triplets.cpp: In function 'll go(int)':
count_triplets.cpp:128:6: warning: unused variable 'pans' [-Wunused-variable]
   ll pans = ans;
      ^~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:175:7: 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:178:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &x, &y);
   ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...