Submission #86161

# Submission time Handle Problem Language Result Execution time Memory
86161 2018-11-25T07:21:11 Z win11905 Torrent (COI16_torrent) C++11
0 / 100
1187 ms 23868 KB
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author win11905
 */

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

const int N = 3e5+5;

class torrent {
private:
	int n, a, b;
	int vl[N], par[N];
	vector<int> g[N];
	void dfs(int u, int p) {
		par[u] = p;
		for(int e : g[u]) if(e != p)
			dfs(vl[e] ^ u, e);
	}
	int dp[N], bad;
	void mic(int u, int p) {
		vector<int> ret;
		for(int e : g[u]) if(e != p && e != bad) {
			mic(vl[e] ^ u, e);
			ret.emplace_back(dp[vl[e] ^ u]);
		}
		sort(all(ret), greater<int>());
		for(int i = 0; i < ret.size(); ++i) dp[u] = max(dp[u], ret[i]+i+1);
	}
	int calc(int val) {
		memset(dp, 0, sizeof dp);
		bad = val;
		mic(a, 0), mic(b, 0);
		return max(dp[a], dp[b]);
	}
public:
	void solve(istream& cin, ostream& cout) {
		cin >> n >> a >> b;
		for(int i = 1, u, v; i < n; ++i) {
		    cin >> u >> v;
		    vl[i] = u ^ v;
		    g[u].emplace_back(i), g[v].emplace_back(i);
		}
		dfs(b, 0);
		vector<int> vec;
		int x = a;
		while(x != b) {
			vec.emplace_back(par[x]);
			x = vl[par[x]] ^ x;
		}
		int l = 0, r = vec.size()-1;
		while(l < r) {
			int m = l + r >> 1;
			if(calc(vec[m]) <= calc(vec[m+1])) r = m;
			else l = m+1;
		}
		cout << calc(vec[r]);
	}
};

class Solver {
public:
	void solve(std::istream& in, std::ostream& out) {
		torrent *obj = new torrent();
		obj->solve(in, out);
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	Solver solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}

Compilation message

torrent.cpp: In member function 'void torrent::mic(int, int)':
torrent.cpp:39:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < ret.size(); ++i) dp[u] = max(dp[u], ret[i]+i+1);
                  ~~^~~~~~~~~~~~
torrent.cpp: In member function 'void torrent::solve(std::istream&, std::ostream&)':
torrent.cpp:64:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    int m = l + r >> 1;
            ~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 11000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1187 ms 23868 KB Output isn't correct
2 Halted 0 ms 0 KB -