Submission #61419

# Submission time Handle Problem Language Result Execution time Memory
61419 2018-07-25T22:00:19 Z kingpig9 Duathlon (APIO18_duathlon) C++11
71 / 100
412 ms 33448 KB
#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;
}

namespace brute_force {
	bool adj[10][10];
	bool dp[1 << 10][10];
	vector<int> gmsk[1 << 10];
	bool can[10][10][10];

	int brute() {
		//can we try to go from here
		for (int i = 0; i < M; i++) {
			int x, y;
			scanf("%d %d", &x, &y);
			x--;
			y--;
			adj[x][y] = adj[y][x] = true;
		}

		for (int c = 0; c < N; c++) {
			for (int i = 0; i < (1 << N); i++) {
				gmsk[i].clear();
			}
			fillchar(dp, 0);

			for (int i = 0; i < (1 << N); i++) {
				for (int j = 0; j < (1 << N); j++) {
					if ((i & j) == (1 << c)) {
						gmsk[i].push_back(j);
					}
				}
			}

			dp[1 << c][c] = true;
			for (int i = 0; i < (1 << N); i++) {
				for (int j = 0; j < N; j++) {
					if (dp[i][j]) {
						//then try to go to a new one
						for (int k = 0; k < N; k++) {
							if (!(i & (1 << k)) && adj[j][k]) {
								dp[i ^ (1 << k)][k] = true;
							}
						}
					}
				}
			}

			for (int i = 0; i < N; i++) {
				if (i != c) {
					for (int j = 0; j < N; j++) {
						if (j != i && j != c) {
							//then see if you can go there
							for (int k = 0; k < (1 << N); k++) {
								if (dp[k][i]) {
									for (int m : gmsk[k]) {
										if (dp[m][j]) {
											can[i][c][j] = true;
										}
									}
								}
							}
						}
					}
				}
			}
		}

		int ans = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				for (int k = 0; k < N; k++) {
					if (i != j && j != k && k != i) {
						ans += can[i][j][k];
					}
				}
			}
		}
		return ans;
	}
}
 
int main() {
	scanf("%d %d", &N, &M);
	if (N <= 10) {
		printf("%d\n", brute_force::brute());
		return 0;
	}
	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);
}

Compilation message

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 brute_force::brute()':
count_triplets.cpp:184:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &x, &y);
    ~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:253: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:260: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 time Memory Grader output
1 Correct 10 ms 8952 KB Output is correct
2 Correct 10 ms 9100 KB Output is correct
3 Correct 9 ms 9100 KB Output is correct
4 Correct 9 ms 9100 KB Output is correct
5 Correct 10 ms 9100 KB Output is correct
6 Correct 42 ms 9292 KB Output is correct
7 Correct 45 ms 9380 KB Output is correct
8 Correct 46 ms 9380 KB Output is correct
9 Correct 54 ms 9380 KB Output is correct
10 Correct 63 ms 9440 KB Output is correct
11 Correct 42 ms 9440 KB Output is correct
12 Correct 44 ms 9476 KB Output is correct
13 Correct 43 ms 9476 KB Output is correct
14 Correct 43 ms 9556 KB Output is correct
15 Correct 44 ms 9556 KB Output is correct
16 Correct 47 ms 9556 KB Output is correct
17 Correct 42 ms 9556 KB Output is correct
18 Correct 53 ms 9556 KB Output is correct
19 Correct 49 ms 9556 KB Output is correct
20 Correct 44 ms 9556 KB Output is correct
21 Correct 53 ms 9556 KB Output is correct
22 Correct 53 ms 9580 KB Output is correct
23 Correct 19 ms 9580 KB Output is correct
24 Correct 11 ms 9580 KB Output is correct
25 Correct 18 ms 9580 KB Output is correct
26 Correct 55 ms 9580 KB Output is correct
27 Correct 13 ms 9580 KB Output is correct
28 Correct 18 ms 9580 KB Output is correct
29 Correct 43 ms 9580 KB Output is correct
30 Correct 13 ms 9580 KB Output is correct
31 Correct 21 ms 9580 KB Output is correct
32 Correct 56 ms 9580 KB Output is correct
33 Correct 23 ms 9580 KB Output is correct
34 Correct 22 ms 9580 KB Output is correct
35 Correct 45 ms 9580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8952 KB Output is correct
2 Correct 10 ms 9100 KB Output is correct
3 Correct 9 ms 9100 KB Output is correct
4 Correct 9 ms 9100 KB Output is correct
5 Correct 10 ms 9100 KB Output is correct
6 Correct 42 ms 9292 KB Output is correct
7 Correct 45 ms 9380 KB Output is correct
8 Correct 46 ms 9380 KB Output is correct
9 Correct 54 ms 9380 KB Output is correct
10 Correct 63 ms 9440 KB Output is correct
11 Correct 42 ms 9440 KB Output is correct
12 Correct 44 ms 9476 KB Output is correct
13 Correct 43 ms 9476 KB Output is correct
14 Correct 43 ms 9556 KB Output is correct
15 Correct 44 ms 9556 KB Output is correct
16 Correct 47 ms 9556 KB Output is correct
17 Correct 42 ms 9556 KB Output is correct
18 Correct 53 ms 9556 KB Output is correct
19 Correct 49 ms 9556 KB Output is correct
20 Correct 44 ms 9556 KB Output is correct
21 Correct 53 ms 9556 KB Output is correct
22 Correct 53 ms 9580 KB Output is correct
23 Correct 19 ms 9580 KB Output is correct
24 Correct 11 ms 9580 KB Output is correct
25 Correct 18 ms 9580 KB Output is correct
26 Correct 55 ms 9580 KB Output is correct
27 Correct 13 ms 9580 KB Output is correct
28 Correct 18 ms 9580 KB Output is correct
29 Correct 43 ms 9580 KB Output is correct
30 Correct 13 ms 9580 KB Output is correct
31 Correct 21 ms 9580 KB Output is correct
32 Correct 56 ms 9580 KB Output is correct
33 Correct 23 ms 9580 KB Output is correct
34 Correct 22 ms 9580 KB Output is correct
35 Correct 45 ms 9580 KB Output is correct
36 Correct 10 ms 9580 KB Output is correct
37 Correct 10 ms 9580 KB Output is correct
38 Incorrect 11 ms 9580 KB Output isn't correct
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 248 ms 26912 KB Output is correct
2 Correct 197 ms 27032 KB Output is correct
3 Correct 278 ms 27032 KB Output is correct
4 Correct 269 ms 27032 KB Output is correct
5 Correct 292 ms 27032 KB Output is correct
6 Correct 300 ms 27032 KB Output is correct
7 Correct 328 ms 27032 KB Output is correct
8 Correct 308 ms 27032 KB Output is correct
9 Correct 303 ms 27032 KB Output is correct
10 Correct 295 ms 27032 KB Output is correct
11 Correct 207 ms 27032 KB Output is correct
12 Correct 233 ms 27032 KB Output is correct
13 Correct 210 ms 27032 KB Output is correct
14 Correct 223 ms 27032 KB Output is correct
15 Correct 177 ms 27032 KB Output is correct
16 Correct 151 ms 27032 KB Output is correct
17 Correct 33 ms 27032 KB Output is correct
18 Correct 28 ms 27032 KB Output is correct
19 Correct 32 ms 27032 KB Output is correct
20 Correct 31 ms 27032 KB Output is correct
21 Correct 26 ms 27032 KB Output is correct
22 Correct 32 ms 27032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 27032 KB Output is correct
2 Correct 15 ms 27032 KB Output is correct
3 Correct 15 ms 27032 KB Output is correct
4 Correct 14 ms 27032 KB Output is correct
5 Correct 14 ms 27032 KB Output is correct
6 Correct 13 ms 27032 KB Output is correct
7 Correct 14 ms 27032 KB Output is correct
8 Correct 15 ms 27032 KB Output is correct
9 Correct 15 ms 27032 KB Output is correct
10 Correct 15 ms 27032 KB Output is correct
11 Correct 15 ms 27032 KB Output is correct
12 Correct 15 ms 27032 KB Output is correct
13 Correct 12 ms 27032 KB Output is correct
14 Correct 14 ms 27032 KB Output is correct
15 Correct 13 ms 27032 KB Output is correct
16 Correct 13 ms 27032 KB Output is correct
17 Correct 13 ms 27032 KB Output is correct
18 Correct 12 ms 27032 KB Output is correct
19 Correct 12 ms 27032 KB Output is correct
20 Correct 12 ms 27032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 345 ms 27032 KB Output is correct
2 Correct 333 ms 27032 KB Output is correct
3 Correct 389 ms 27032 KB Output is correct
4 Correct 347 ms 27032 KB Output is correct
5 Correct 325 ms 27032 KB Output is correct
6 Correct 365 ms 33448 KB Output is correct
7 Correct 400 ms 33448 KB Output is correct
8 Correct 369 ms 33448 KB Output is correct
9 Correct 369 ms 33448 KB Output is correct
10 Correct 365 ms 33448 KB Output is correct
11 Correct 376 ms 33448 KB Output is correct
12 Correct 366 ms 33448 KB Output is correct
13 Correct 388 ms 33448 KB Output is correct
14 Correct 283 ms 33448 KB Output is correct
15 Correct 282 ms 33448 KB Output is correct
16 Correct 187 ms 33448 KB Output is correct
17 Correct 267 ms 33448 KB Output is correct
18 Correct 277 ms 33448 KB Output is correct
19 Correct 291 ms 33448 KB Output is correct
20 Correct 293 ms 33448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 33448 KB Output is correct
2 Correct 14 ms 33448 KB Output is correct
3 Correct 14 ms 33448 KB Output is correct
4 Correct 14 ms 33448 KB Output is correct
5 Correct 15 ms 33448 KB Output is correct
6 Correct 14 ms 33448 KB Output is correct
7 Correct 12 ms 33448 KB Output is correct
8 Correct 15 ms 33448 KB Output is correct
9 Correct 14 ms 33448 KB Output is correct
10 Correct 13 ms 33448 KB Output is correct
11 Correct 14 ms 33448 KB Output is correct
12 Correct 17 ms 33448 KB Output is correct
13 Correct 14 ms 33448 KB Output is correct
14 Correct 14 ms 33448 KB Output is correct
15 Correct 14 ms 33448 KB Output is correct
16 Correct 12 ms 33448 KB Output is correct
17 Correct 15 ms 33448 KB Output is correct
18 Correct 14 ms 33448 KB Output is correct
19 Correct 15 ms 33448 KB Output is correct
20 Correct 14 ms 33448 KB Output is correct
21 Correct 15 ms 33448 KB Output is correct
22 Correct 17 ms 33448 KB Output is correct
23 Correct 15 ms 33448 KB Output is correct
24 Correct 12 ms 33448 KB Output is correct
25 Correct 13 ms 33448 KB Output is correct
26 Correct 12 ms 33448 KB Output is correct
27 Correct 12 ms 33448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 33448 KB Output is correct
2 Correct 349 ms 33448 KB Output is correct
3 Correct 400 ms 33448 KB Output is correct
4 Correct 352 ms 33448 KB Output is correct
5 Correct 283 ms 33448 KB Output is correct
6 Correct 284 ms 33448 KB Output is correct
7 Correct 254 ms 33448 KB Output is correct
8 Correct 229 ms 33448 KB Output is correct
9 Correct 233 ms 33448 KB Output is correct
10 Correct 250 ms 33448 KB Output is correct
11 Correct 255 ms 33448 KB Output is correct
12 Correct 235 ms 33448 KB Output is correct
13 Correct 243 ms 33448 KB Output is correct
14 Correct 250 ms 33448 KB Output is correct
15 Correct 412 ms 33448 KB Output is correct
16 Correct 382 ms 33448 KB Output is correct
17 Correct 382 ms 33448 KB Output is correct
18 Correct 387 ms 33448 KB Output is correct
19 Correct 340 ms 33448 KB Output is correct
20 Correct 360 ms 33448 KB Output is correct
21 Correct 349 ms 33448 KB Output is correct
22 Correct 271 ms 33448 KB Output is correct
23 Correct 261 ms 33448 KB Output is correct
24 Correct 344 ms 33448 KB Output is correct
25 Correct 350 ms 33448 KB Output is correct
26 Correct 338 ms 33448 KB Output is correct
27 Correct 296 ms 33448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8952 KB Output is correct
2 Correct 10 ms 9100 KB Output is correct
3 Correct 9 ms 9100 KB Output is correct
4 Correct 9 ms 9100 KB Output is correct
5 Correct 10 ms 9100 KB Output is correct
6 Correct 42 ms 9292 KB Output is correct
7 Correct 45 ms 9380 KB Output is correct
8 Correct 46 ms 9380 KB Output is correct
9 Correct 54 ms 9380 KB Output is correct
10 Correct 63 ms 9440 KB Output is correct
11 Correct 42 ms 9440 KB Output is correct
12 Correct 44 ms 9476 KB Output is correct
13 Correct 43 ms 9476 KB Output is correct
14 Correct 43 ms 9556 KB Output is correct
15 Correct 44 ms 9556 KB Output is correct
16 Correct 47 ms 9556 KB Output is correct
17 Correct 42 ms 9556 KB Output is correct
18 Correct 53 ms 9556 KB Output is correct
19 Correct 49 ms 9556 KB Output is correct
20 Correct 44 ms 9556 KB Output is correct
21 Correct 53 ms 9556 KB Output is correct
22 Correct 53 ms 9580 KB Output is correct
23 Correct 19 ms 9580 KB Output is correct
24 Correct 11 ms 9580 KB Output is correct
25 Correct 18 ms 9580 KB Output is correct
26 Correct 55 ms 9580 KB Output is correct
27 Correct 13 ms 9580 KB Output is correct
28 Correct 18 ms 9580 KB Output is correct
29 Correct 43 ms 9580 KB Output is correct
30 Correct 13 ms 9580 KB Output is correct
31 Correct 21 ms 9580 KB Output is correct
32 Correct 56 ms 9580 KB Output is correct
33 Correct 23 ms 9580 KB Output is correct
34 Correct 22 ms 9580 KB Output is correct
35 Correct 45 ms 9580 KB Output is correct
36 Correct 10 ms 9580 KB Output is correct
37 Correct 10 ms 9580 KB Output is correct
38 Incorrect 11 ms 9580 KB Output isn't correct
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8952 KB Output is correct
2 Correct 10 ms 9100 KB Output is correct
3 Correct 9 ms 9100 KB Output is correct
4 Correct 9 ms 9100 KB Output is correct
5 Correct 10 ms 9100 KB Output is correct
6 Correct 42 ms 9292 KB Output is correct
7 Correct 45 ms 9380 KB Output is correct
8 Correct 46 ms 9380 KB Output is correct
9 Correct 54 ms 9380 KB Output is correct
10 Correct 63 ms 9440 KB Output is correct
11 Correct 42 ms 9440 KB Output is correct
12 Correct 44 ms 9476 KB Output is correct
13 Correct 43 ms 9476 KB Output is correct
14 Correct 43 ms 9556 KB Output is correct
15 Correct 44 ms 9556 KB Output is correct
16 Correct 47 ms 9556 KB Output is correct
17 Correct 42 ms 9556 KB Output is correct
18 Correct 53 ms 9556 KB Output is correct
19 Correct 49 ms 9556 KB Output is correct
20 Correct 44 ms 9556 KB Output is correct
21 Correct 53 ms 9556 KB Output is correct
22 Correct 53 ms 9580 KB Output is correct
23 Correct 19 ms 9580 KB Output is correct
24 Correct 11 ms 9580 KB Output is correct
25 Correct 18 ms 9580 KB Output is correct
26 Correct 55 ms 9580 KB Output is correct
27 Correct 13 ms 9580 KB Output is correct
28 Correct 18 ms 9580 KB Output is correct
29 Correct 43 ms 9580 KB Output is correct
30 Correct 13 ms 9580 KB Output is correct
31 Correct 21 ms 9580 KB Output is correct
32 Correct 56 ms 9580 KB Output is correct
33 Correct 23 ms 9580 KB Output is correct
34 Correct 22 ms 9580 KB Output is correct
35 Correct 45 ms 9580 KB Output is correct
36 Correct 10 ms 9580 KB Output is correct
37 Correct 10 ms 9580 KB Output is correct
38 Incorrect 11 ms 9580 KB Output isn't correct
39 Halted 0 ms 0 KB -