답안 #226618

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
226618 2020-04-24T14:43:35 Z stefdasca City Mapping (NOI18_citymapping) C++14
0 / 100
21 ms 640 KB
#include "citymapping.h"
#include <bits/stdc++.h>
using namespace std;

long long dist[1002];

/*
long long oxy[1002][1002];
long long get_distance(int a, int b)
{
	return oxy[a][b];
}
*/
bool cmp(int a, int b)
{
	return dist[a] < dist[b];
}
int rad = 1;
vector<int> v[1002];
int sts[1002], nu[1002];
void dfs(int dad, int nod)
{
	sts[nod] = 1;
	for(int i = 0; i < v[nod].size(); ++i)
	{
		int vecin = v[nod][i];
		if(vecin == dad)
			continue;
		dfs(nod, vecin);
		sts[nod] += sts[vecin];
	}
}
int bgL(int nod)
{
	bool ok = 1;
	int bst = 0;
	for(int i = 0; i < v[nod].size(); ++i)
	{
		int vecin = v[nod][i];
		if(sts[vecin] > sts[nod] || nu[vecin])
			continue;
		ok = 0;
		if(sts[vecin] > sts[bst])
			bst = vecin;
	}
	if(ok)
		return nod;
	return bgL(bst);
}
int bgg;
int optnd(int nod, long long dst)
{
	if(dist[nod] - dist[bgg] == dst)
		return nod;
	int bst = 0;
	for(int i = 0; i < v[nod].size(); ++i)
	{
		int vecin = v[nod][i];
		if(sts[vecin] > sts[nod] || nu[vecin])
			continue;
		if(sts[vecin] > sts[bst])
			bst = vecin;
	}
	return optnd(bst, dst);
}
int bstson(int nod)
{
	int bst = 0;
	for(int i = 0; i < v[nod].size(); ++i)
	{
		int vecin = v[nod][i];
		if(sts[vecin] > sts[nod] || nu[vecin])
			continue;
		if(sts[vecin] > sts[bst])
			bst = vecin;
	}
	return bst;
}
int dfssz(int rt)
{
	int ans = 1;
	for(int i = 0; i < v[rt].size(); ++i)
	{
		int vecin = v[rt][i];
		if(sts[vecin] > sts[rt] || nu[vecin])
			continue;
		ans += dfssz(vecin);
	}
	return ans;
}
pair<int, int> solve(int nod, int pzx)
{
	int rt = 1;
	int lft = pzx;
	while(lft > 1)
	{
		int ans = bgL(rt);
		long long RL = abs(dist[ans] - dist[rt]);
		long long LX = get_distance(nod, ans);
		long long RX = abs(dist[nod] - dist[rt]);
		long long dvg = ((RL+RX) - LX) / 2;
		bgg = rt;
		int nxt = optnd(rt, dvg);
		//cout << "ORZ " << rt << " " << dvg << " " << nod << " " << nxt <<'\n';
		//cout << bstson(nxt) << '\n';
		nu[bstson(nxt)] = 1;
		rt = nxt;
		lft = dfssz(rt);
		cout << rt << " " << lft << '\n';
	}
	//cout <<"EDG " << nod << " " << dist[nod] << " " << dist[rt] << '\n';
	return {rt, dist[nod] - dist[rt]};
}
void find_roads(int n, int Q, int A[], int B[], int W[])
{
	dist[1] = 0;
	for(int i = 2; i <= n; ++i)
		dist[i] = get_distance(1, i);	
	int nr[1002];
	for(int i = 1; i <= n; ++i)
		nr[i] = i;
	sort(nr + 1, nr + n + 1, cmp);
	for(int i = 2; i <= n; ++i)
	{
		A[i-1] = nr[i];
		if(i != 2)
		{
			pair<int, int> ans = solve(nr[i], i-1);
			B[i-1] = ans.first;
			W[i-1] = ans.second;
		}
		else
		{
			B[i-1] = 1;
			W[i-1] = dist[nr[i]];
		}
		v[A[i-1]].push_back(B[i-1]);
		v[B[i-1]].push_back(A[i-1]);
		dfs(0, 1);
		memset(nu, 0, sizeof(nu));
	}
	return;
}

Compilation message

citymapping.cpp: In function 'void dfs(int, int)':
citymapping.cpp:24:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v[nod].size(); ++i)
                 ~~^~~~~~~~~~~~~~~
citymapping.cpp: In function 'int bgL(int)':
citymapping.cpp:37:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v[nod].size(); ++i)
                 ~~^~~~~~~~~~~~~~~
citymapping.cpp: In function 'int optnd(int, long long int)':
citymapping.cpp:56:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v[nod].size(); ++i)
                 ~~^~~~~~~~~~~~~~~
citymapping.cpp: In function 'int bstson(int)':
citymapping.cpp:69:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v[nod].size(); ++i)
                 ~~^~~~~~~~~~~~~~~
citymapping.cpp: In function 'int dfssz(int)':
citymapping.cpp:82:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v[rt].size(); ++i)
                 ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 640 KB DO NOT PRINT ANYTHING TO STANDARD OUTPUT
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 640 KB DO NOT PRINT ANYTHING TO STANDARD OUTPUT
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 640 KB DO NOT PRINT ANYTHING TO STANDARD OUTPUT
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 640 KB DO NOT PRINT ANYTHING TO STANDARD OUTPUT
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 640 KB DO NOT PRINT ANYTHING TO STANDARD OUTPUT
2 Halted 0 ms 0 KB -