제출 #69459

#제출 시각아이디문제언어결과실행 시간메모리
69459kingpig9Network (BOI15_net)C++11
100 / 100
698 ms154532 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

#define debug(...) fprintf(stderr, __VA_ARGS__)
//#define debug(...)
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
#define fi first
#define se second

int N;
vector<int> adj[500010];
vector<int> leaves;

void dfs (int x, int p) {
	if (adj[x].size() == 1) {
		leaves.push_back(x);
	}
	for (int y : adj[x]) {
		if (y != p) {
			dfs(y, x);
		}
	}
}

int main() {
	scanf("%d", &N);
	for (int i = 1; i < N; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	int root = 1;
	while (adj[root].size() == 1) {
		root++;
	}
	dfs(root, -1);

	vector<pii> ans;

	for (int i = 0, j = leaves.size() / 2; j < leaves.size(); i++, j++) {
		ans.push_back({leaves[i], leaves[j]});
	}

	printf("%lu\n", ans.size());
	for (pii p : ans) {
		printf("%d %d\n", p.fi, p.se);
	}
}

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

net.cpp: In function 'int main()':
net.cpp:46:43: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0, j = leaves.size() / 2; j < leaves.size(); i++, j++) {
                                         ~~^~~~~~~~~~~~~~~
net.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
net.cpp:33: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...