답안 #57772

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57772 2018-07-16T04:13:14 Z Crown 철인 이종 경기 (APIO18_duathlon) C++14
31 / 100
363 ms 57080 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)];

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)
{
	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(cnt[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:133: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:136: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 16760 KB Output is correct
2 Correct 16 ms 16872 KB Output is correct
3 Correct 24 ms 16872 KB Output is correct
4 Correct 19 ms 16924 KB Output is correct
5 Correct 18 ms 16984 KB Output is correct
6 Correct 22 ms 16984 KB Output is correct
7 Incorrect 16 ms 16984 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 16 ms 16872 KB Output is correct
3 Correct 24 ms 16872 KB Output is correct
4 Correct 19 ms 16924 KB Output is correct
5 Correct 18 ms 16984 KB Output is correct
6 Correct 22 ms 16984 KB Output is correct
7 Incorrect 16 ms 16984 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 198 ms 36824 KB Output is correct
2 Correct 190 ms 36824 KB Output is correct
3 Correct 183 ms 40572 KB Output is correct
4 Correct 269 ms 40572 KB Output is correct
5 Correct 302 ms 40572 KB Output is correct
6 Correct 185 ms 44672 KB Output is correct
7 Correct 302 ms 44672 KB Output is correct
8 Correct 286 ms 44672 KB Output is correct
9 Correct 268 ms 44672 KB Output is correct
10 Correct 291 ms 44672 KB Output is correct
11 Correct 243 ms 44672 KB Output is correct
12 Correct 212 ms 44672 KB Output is correct
13 Correct 189 ms 44672 KB Output is correct
14 Correct 186 ms 44672 KB Output is correct
15 Correct 102 ms 44672 KB Output is correct
16 Correct 96 ms 44672 KB Output is correct
17 Correct 19 ms 44672 KB Output is correct
18 Correct 19 ms 44672 KB Output is correct
19 Correct 18 ms 44672 KB Output is correct
20 Correct 19 ms 44672 KB Output is correct
21 Correct 19 ms 44672 KB Output is correct
22 Correct 18 ms 44672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 44672 KB Output is correct
2 Correct 16 ms 44672 KB Output is correct
3 Correct 21 ms 44672 KB Output is correct
4 Correct 20 ms 44672 KB Output is correct
5 Correct 19 ms 44672 KB Output is correct
6 Correct 16 ms 44672 KB Output is correct
7 Correct 16 ms 44672 KB Output is correct
8 Correct 16 ms 44672 KB Output is correct
9 Correct 16 ms 44672 KB Output is correct
10 Correct 16 ms 44672 KB Output is correct
11 Correct 19 ms 44672 KB Output is correct
12 Correct 19 ms 44672 KB Output is correct
13 Correct 19 ms 44672 KB Output is correct
14 Correct 16 ms 44672 KB Output is correct
15 Correct 16 ms 44672 KB Output is correct
16 Correct 16 ms 44672 KB Output is correct
17 Correct 16 ms 44672 KB Output is correct
18 Correct 16 ms 44672 KB Output is correct
19 Correct 17 ms 44672 KB Output is correct
20 Correct 16 ms 44672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 44672 KB Output is correct
2 Correct 303 ms 44672 KB Output is correct
3 Correct 317 ms 44672 KB Output is correct
4 Correct 325 ms 44672 KB Output is correct
5 Correct 309 ms 44672 KB Output is correct
6 Correct 266 ms 57080 KB Output is correct
7 Correct 321 ms 57080 KB Output is correct
8 Correct 345 ms 57080 KB Output is correct
9 Correct 340 ms 57080 KB Output is correct
10 Correct 301 ms 57080 KB Output is correct
11 Correct 303 ms 57080 KB Output is correct
12 Correct 278 ms 57080 KB Output is correct
13 Correct 304 ms 57080 KB Output is correct
14 Correct 245 ms 57080 KB Output is correct
15 Correct 213 ms 57080 KB Output is correct
16 Correct 138 ms 57080 KB Output is correct
17 Correct 180 ms 57080 KB Output is correct
18 Correct 139 ms 57080 KB Output is correct
19 Correct 205 ms 57080 KB Output is correct
20 Correct 155 ms 57080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 57080 KB Output is correct
2 Correct 17 ms 57080 KB Output is correct
3 Incorrect 21 ms 57080 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 244 ms 57080 KB Output is correct
2 Correct 363 ms 57080 KB Output is correct
3 Incorrect 246 ms 57080 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 16 ms 16872 KB Output is correct
3 Correct 24 ms 16872 KB Output is correct
4 Correct 19 ms 16924 KB Output is correct
5 Correct 18 ms 16984 KB Output is correct
6 Correct 22 ms 16984 KB Output is correct
7 Incorrect 16 ms 16984 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16760 KB Output is correct
2 Correct 16 ms 16872 KB Output is correct
3 Correct 24 ms 16872 KB Output is correct
4 Correct 19 ms 16924 KB Output is correct
5 Correct 18 ms 16984 KB Output is correct
6 Correct 22 ms 16984 KB Output is correct
7 Incorrect 16 ms 16984 KB Output isn't correct
8 Halted 0 ms 0 KB -