Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const long long maxn = 1e3+10;
- const long long mod = 1e9+7;
- int main()
- {
- ios::sync_with_stdio(false);
- long long n,k;
- cin>>n>>k;
- long long a[n];
- for(long long i=0;i<n;i++)
- {
- cin>>a[i];
- }
- vector<int>g[maxn];
- for(long long i=0;i<n-1;i++)
- {
- long long x,y;
- cin>>x>>y;
- x--;y--;
- g[x].push_back(y);
- g[y].push_back(x);
- }
- long long mindis[maxn];
- for(long long i=0;i<n;i++)
- {
- bool vis[n];
- memset(vis,0,sizeof vis);
- queue<int>Q;
- Q.push(i);
- Q.push(0);
- long long mx=0;
- long long idx=2e9;
- vis[i]=true;
- while(!Q.empty())
- {
- long long teme=Q.front();Q.pop();
- long long kol=Q.front();Q.pop();
- for(long long ax:g[teme])
- {
- if(vis[ax])continue;
- if(mx<a[ax]-kol)
- {
- mx=a[ax]-kol;
- idx=ax;
- }
- else if(mx==a[ax]-kol)
- {
- idx=min(idx, ax);
- }
- Q.push(ax);
- Q.push(kol+1);
- vis[ax]=true;
- }
- }
- mindis[i]=idx;
- }
- if(n<=2000 && k<=2000)
- {
- queue<int>ans;
- ans.push(0);
- while(k--)
- {
- long long node=ans.front();ans.pop();
- long long minsosed=mindis[node];
- ans.push(minsosed);
- }
- cout<<ans.front()+1<<endl;
- return 0;
- }
- bool vis[maxn];
- memset(vis,0,sizeof vis);
- vis[0]=true;
- queue<int>q;
- q.push(0);
- long long pat=0;
- long long dis[maxn];
- memset(dis,0,sizeof dis);
- while(k--)
- {
- long long node=q.front();q.pop();
- pat++;
- long long minsosed=mindis[node];
- q.push(minsosed);
- if(!vis[minsosed])
- {
- dis[minsosed]=pat;
- vis[minsosed] = true;
- }
- else
- {
- long long D=pat-dis[minsosed];///kolku e dolg ciklusot
- long long kolku=k/D;
- k-=kolku*D;
- }
- }
- cout<<q.front() + 1<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement