제출 #878685

#제출 시각아이디문제언어결과실행 시간메모리
878685Faisal_SaqibMousetrap (CEOI17_mousetrap)C++17
25 / 100
1353 ms147312 KiB
#include <iostream>
#include <vector>
#include <set>
using namespace std;
const int N=1e6+2;
set<int> ma[N];
int dp_to_t[N];
int dp[N]; // dp[x] = def = what is the minimum number of step(Of Kalu ustad) return back to x,such that the subtree of x  are  the only cities you can go to
int n,t,m;
int from,to;
vector<int> path,prefix;
void find_path(int x,int p=-1)
{
	prefix.push_back(x);
	if(to==x)
	{
		while(prefix.back()!=from)
		{
			path.push_back(prefix.back());
			prefix.pop_back();
		}
		int i=((int)path.size())-1;
		path.push_back(from);
		while(i>=0 and prefix.back()!=to)
		{
			prefix.push_back(path[i]);
			i--;
		}
	}
	for(auto y:ma[x])
		if(y!=p)
			find_path(y,x);
	prefix.pop_back();
}
void dfs(int x,int p=-1)
{
	int total=0;
	for(auto y:ma[x])
	{
		if(y!=p)
		{
			dfs(y,x);
			total++;
		}
	}
	int mx=0;
	int mx2=0;
	for(auto y:ma[x])
	{
		if(y!=p)
		{
			if(dp[y]>mx)
			{
				mx2=mx;
				mx=dp[y];
			}
			else if(dp[y]>mx2)
			{
				mx2=dp[y];
			}
		}
	}
	dp[x]=mx2+total;
}
int main()
{
	cin>>n>>t>>m;
	for(int i=1;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		ma[x].insert(y);
		ma[y].insert(x);
	}
	from=t;
	to=m;
	find_path(from);
	long long ansp=0;
	for(int i=0;i+1<path.size();i++)
	{
		// Remove edge 
		int x=path[i+1];
		int y=path[i];
		ma[x].erase(y);
		ma[y].erase(x);
		dfs(y);
		ansp+=dp[y];
		// cout<<dp[y]<<endl;
	}
	cout<<ansp<<endl;
	// cout<<endl;
	// cout<<dp[m]<<endl;
	// for(int i=1;i<=n;i++)
	// {
	// 	cout<<"for" <<i<<' '<<dp[i]<<endl;
	// }
	return 0;
}

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

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:79:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |  for(int i=0;i+1<path.size();i++)
      |              ~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...