G题 95%在线蹲一个有缘人救命
找了114514年
对拍也拍不出来
多久时间后我能蹲一个救命恩人
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<set>
#include<cassert>
#include<map>
using namespace std;
// #define debug(x) cout<<"[debug]"#x<<"="<<x<<endl
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ld eps=1e-8;
const int INF=0x3f3f3f3f,mod=998244353;
const ll INFF=0x3f3f3f3f3f3f3f3f;
#ifndef ONLINE_JUDGE
#define debug(...)
#include<debug>
#else
#define debug(...)
#endif
const int N=200005;
map<int,int> dp[N];
int n,k;
vector<pii> edg[N];
int res=-1;
void dfs(int u)
{
auto cal=[&](int d,int c){
if(c>=k) res=max(res,d);
};
auto update=[&](int d,int c){
auto it=dp[u].find(d);
if(it==dp[u].end())
{
dp[u][d]=c;
cal(d,c);
}
else
{
cal(d,it->second+c-1);
if(c>it->second) dp[u][d]=c;
}
};
for(auto [v,w]:edg[u])
{
dfs(v);
for(auto [x,y]:dp[v])
{
int d=__gcd(w,x);
update(d,y+1);
}
}
update(0,1);
}
int main()
{
scanf("%d%d",&n,&k);
// if(n==2) assert(0);
for(int i=2;i<=n;i++)
{
int p,w;
scanf("%d%d",&p,&w);
edg[p].push_back({i,w});
}
dfs(1);
if(res==-1)
{
for(int i=1;i<=n;i++)
{
int ma1=0,ma2=0;
for(auto [x,y]:dp[i])
{
if(y>ma1) ma2=ma1,ma1=y;
else if(y>ma2) ma2=y;
}
if(ma2&&ma1+ma2-1>=k) res=1;
}
}
printf("%d\n",res);
}

