답안 #57774

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57774 2018-07-16T04:19:23 Z Crown 철인 이종 경기 (APIO18_duathlon) C++14
31 / 100
395 ms 54572 KB
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 1e5+5;
const int maxm = 2e5+5;

int n, m; 

vector< ii > adj[maxn];
int U[maxm], V[maxm];
int num[maxn], low[maxn];
int now = 1;
bool instack[maxm];
stack<int> s;
int compno;
int col[maxm];

vector<int> tree[2*(maxn+maxm)];
int cnt[2*(maxn+maxm)];
bool use[maxn+maxm];

void tarjan(int u, int p)
{
	num[u] = low[u] = now;
	now++;
	for(auto earn : adj[u])
	{
		int v = earn.X, id = earn.Y;
		if(!num[v])
		{
			if(!instack[id])
			{
				s.push(id);
				instack[id] = true;
			}
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
			if(low[v]>= num[u])
			{
				// u is an articulation point
				compno++;
				while(!s.empty())
				{
					int x = s.top(); s.pop();
					col[x] = compno;
					if(x == id) break;
				}
			}
		}
		else if(v != p)
		{
			if(!instack[id])
			{
				s.push(id);
				instack[id] = true;
			}
			low[u] = min(low[u], num[v]);
		}
	}
}

ll res;

void dfs(int u, int p)
{
	use[u] = true;
	cnt[u] = u>compno;
	for(auto v : tree[u])
	{
		if(v == p) continue;
		dfs(v, u);
		cnt[u] += cnt[v];
	}
}

void solve(int u, int p, int par_cnt)
{
	//printf("%d (%d)\n", u, par_cnt);
	if(u> compno)
	{
		ll tot = 0;
		ll sq = 0;
		for(auto v : tree[u])
		{
			if(v == p) continue;
			tot += cnt[v];
			sq += 1LL*cnt[v]*cnt[v];
		}
		res += tot*tot-sq;
	}
	else
	{
		ll tot = 0;
		ll sq = 0;
		vector<int> ne;
		for(auto v : tree[u])
		{
			if(v == p) continue;
			if(!cnt[v]) continue;
			ne.pb(cnt[v]);
			tot += cnt[v];
			sq += 1LL*cnt[v]*cnt[v];
		}
		//printf("tot is %lld sq is %lld\n", tot, sq);
		if(par_cnt)
		{
			tot += par_cnt; sq += 1LL*par_cnt*par_cnt;
		}
		for(auto x : ne)
		{
			res += (tot-1)*(tot-1) - (sq - (2LL*x - 1) );
		}
	}
	//printf("res is now %lld\n", res);
	int tot = u> compno;
	for(auto v : tree[u])
	{
		if(v == p) continue;
		tot += cnt[v];
	}
	for(auto v : tree[u])
	{
		if(v == p) continue;
		solve(v, u, par_cnt+tot-cnt[v]);
	}
}

int main()
{
	scanf("%d %d", &n, &m);
	for(int i = 1; i<= m; i++)
	{
		int u, v; scanf("%d %d", &u, &v);
		U[i] = u; V[i] = v;
		adj[u].pb(ii(v, i));
		adj[v].pb(ii(u, i));
	}
	for(int i = 1; i<= n; i++)
	{
		if(num[i]) continue;
		tarjan(i, 0);
		if(!s.empty())
		{
			compno++;
			while(!s.empty())
			{
				int x = s.top(); s.pop();
				col[x] = compno;
			}
		}
	}
	for(int i = 1; i<= m; i++)
	{
		int x = col[i];
		int u = U[i], v = V[i];
		tree[compno+u].pb(x);
		tree[x].pb(compno+u);
		tree[compno+v].pb(x);
		tree[x].pb(compno+v);
	}
	for(int i = 1; i<= compno+n; i++)
	{
		sort(tree[i].begin(), tree[i].end());
		tree[i].resize(unique(tree[i].begin(), tree[i].end())-tree[i].begin());
	}
	for(int i = 1; i<= compno+n; i++)
	{
		if(use[i]) continue;
		dfs(i, 0);
		solve(i, 0, 0);
	}
	printf("%lld\n", res);
}

Compilation message

count_triplets.cpp: In function 'int main()':
count_triplets.cpp:135: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:138:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; scanf("%d %d", &u, &v);
             ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16764 KB Output is correct
2 Correct 16 ms 16868 KB Output is correct
3 Correct 15 ms 16868 KB Output is correct
4 Correct 16 ms 16868 KB Output is correct
5 Correct 17 ms 16868 KB Output is correct
6 Correct 15 ms 17048 KB Output is correct
7 Incorrect 17 ms 17048 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16764 KB Output is correct
2 Correct 16 ms 16868 KB Output is correct
3 Correct 15 ms 16868 KB Output is correct
4 Correct 16 ms 16868 KB Output is correct
5 Correct 17 ms 16868 KB Output is correct
6 Correct 15 ms 17048 KB Output is correct
7 Incorrect 17 ms 17048 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 232 ms 36984 KB Output is correct
2 Correct 226 ms 36984 KB Output is correct
3 Correct 309 ms 40668 KB Output is correct
4 Correct 255 ms 40668 KB Output is correct
5 Correct 275 ms 40668 KB Output is correct
6 Correct 339 ms 42204 KB Output is correct
7 Correct 307 ms 42204 KB Output is correct
8 Correct 335 ms 42204 KB Output is correct
9 Correct 332 ms 42204 KB Output is correct
10 Correct 300 ms 42204 KB Output is correct
11 Correct 243 ms 42204 KB Output is correct
12 Correct 232 ms 42204 KB Output is correct
13 Correct 219 ms 42204 KB Output is correct
14 Correct 145 ms 42204 KB Output is correct
15 Correct 150 ms 42204 KB Output is correct
16 Correct 165 ms 42204 KB Output is correct
17 Correct 22 ms 42204 KB Output is correct
18 Correct 27 ms 42204 KB Output is correct
19 Correct 22 ms 42204 KB Output is correct
20 Correct 22 ms 42204 KB Output is correct
21 Correct 22 ms 42204 KB Output is correct
22 Correct 22 ms 42204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 42204 KB Output is correct
2 Correct 19 ms 42204 KB Output is correct
3 Correct 20 ms 42204 KB Output is correct
4 Correct 19 ms 42204 KB Output is correct
5 Correct 18 ms 42204 KB Output is correct
6 Correct 20 ms 42204 KB Output is correct
7 Correct 17 ms 42204 KB Output is correct
8 Correct 19 ms 42204 KB Output is correct
9 Correct 19 ms 42204 KB Output is correct
10 Correct 16 ms 42204 KB Output is correct
11 Correct 17 ms 42204 KB Output is correct
12 Correct 16 ms 42204 KB Output is correct
13 Correct 16 ms 42204 KB Output is correct
14 Correct 19 ms 42204 KB Output is correct
15 Correct 20 ms 42204 KB Output is correct
16 Correct 19 ms 42204 KB Output is correct
17 Correct 22 ms 42204 KB Output is correct
18 Correct 19 ms 42204 KB Output is correct
19 Correct 19 ms 42204 KB Output is correct
20 Correct 19 ms 42204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 42204 KB Output is correct
2 Correct 356 ms 42204 KB Output is correct
3 Correct 263 ms 42204 KB Output is correct
4 Correct 247 ms 42204 KB Output is correct
5 Correct 334 ms 42204 KB Output is correct
6 Correct 354 ms 54572 KB Output is correct
7 Correct 335 ms 54572 KB Output is correct
8 Correct 262 ms 54572 KB Output is correct
9 Correct 286 ms 54572 KB Output is correct
10 Correct 363 ms 54572 KB Output is correct
11 Correct 395 ms 54572 KB Output is correct
12 Correct 337 ms 54572 KB Output is correct
13 Correct 316 ms 54572 KB Output is correct
14 Correct 270 ms 54572 KB Output is correct
15 Correct 267 ms 54572 KB Output is correct
16 Correct 101 ms 54572 KB Output is correct
17 Correct 193 ms 54572 KB Output is correct
18 Correct 195 ms 54572 KB Output is correct
19 Correct 215 ms 54572 KB Output is correct
20 Correct 215 ms 54572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 54572 KB Output is correct
2 Correct 16 ms 54572 KB Output is correct
3 Incorrect 17 ms 54572 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 345 ms 54572 KB Output is correct
2 Correct 245 ms 54572 KB Output is correct
3 Incorrect 275 ms 54572 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16764 KB Output is correct
2 Correct 16 ms 16868 KB Output is correct
3 Correct 15 ms 16868 KB Output is correct
4 Correct 16 ms 16868 KB Output is correct
5 Correct 17 ms 16868 KB Output is correct
6 Correct 15 ms 17048 KB Output is correct
7 Incorrect 17 ms 17048 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16764 KB Output is correct
2 Correct 16 ms 16868 KB Output is correct
3 Correct 15 ms 16868 KB Output is correct
4 Correct 16 ms 16868 KB Output is correct
5 Correct 17 ms 16868 KB Output is correct
6 Correct 15 ms 17048 KB Output is correct
7 Incorrect 17 ms 17048 KB Output isn't correct
8 Halted 0 ms 0 KB -