Submission #113185

# Submission time Handle Problem Language Result Execution time Memory
113185 2019-05-24T08:07:17 Z davitmarg Network (BOI15_net) C++17
0 / 100
4 ms 2688 KB
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <ctype.h>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin (),v.end()
using namespace std;

int n,used[100005],start;
vector<int> g[100005],a,b;
vector<pair<int, int>> ans;

void dfs(int v)
{
	used[v] = 1;
	if (g[v].size() == 1)
	{
		if(a.empty())
			b.PB(v);
		else
		{
			ans.PB(MP(a.back(), v));
			a.pop_back();
		}
	}
	for (int i = 0; i < g[v].size(); i++)
	{
		int to = g[v][i];
		if (used[to])
			continue;
		dfs(to);
		if (v == start)
		{
			while (!b.empty())
			{
				a.PB(b.back());
				b.pop_back();
			}
		}
	}
}

int main()
{
	cin >> n;
	if (n == 0)
	{
		printf("0\n");
		return 0;
	}
	else if (n == 2)
	{
		printf("1\n");
		printf("1 2\n");
		return 0;
	}
	for (int i = 1; i < n; i++)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		g[a].PB(b);
		g[b].PB(a);
	}
	for(int i=1;i<=n;i++)
		if (g[i].size() != 1)
		{
			start = i;
			break;
		}
	dfs(start);
	if (!a.empty())
		if (a[0] == 1)
			ans.PB(MP(1, 2));
		else
			ans.PB(MP(1, a[0]));

	printf("%d\n",(int)ans.size());
	for (int i = 0; i < ans.size(); i++)
		printf("%d %d\n", ans[i].first, ans[i].second);

	return 0;
}

/*



*/

Compilation message

net.cpp: In function 'void dfs(int)':
net.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < g[v].size(); i++)
                  ~~^~~~~~~~~~~~~
net.cpp: In function 'int main()':
net.cpp:88:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
  if (!a.empty())
     ^
net.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < ans.size(); i++)
                  ~~^~~~~~~~~~~~
net.cpp:77:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Incorrect 3 ms 2688 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Incorrect 3 ms 2688 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Incorrect 3 ms 2688 KB Breaking single line is causing network to disconnect.
3 Halted 0 ms 0 KB -