#include<bits/stdc++.h>#define int long long#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;usingpii=pair<int,int>;constintINF=2e18;constintmaxn=1e5+5;intn,m;vector<int>G[maxn];intfrom[maxn];boolvis[maxn],instk[maxn];stack<int>cycle;voidget_cycle(ints,intt){intx=t;cycle.push(s);while(x!=s){cycle.push(x);x=from[x];}cycle.push(s);}booldfs(intu){instk[u]=vis[u]=true;for(autov:G[u]){if(vis[v]==false){from[v]=u;if(dfs(v))returntrue;}elseif(instk[v]){get_cycle(v,u);returntrue;}}instk[u]=false;returnfalse;}signedmain(){cin>>n>>m;for(inti=0;i<m;i++){intu,v;cin>>u>>v;G[u].push_back(v);}for(inti=1;i<=n;i++){if(vis[i]==false){if(dfs(i)){cout<<cycle.size()<<'\n';while(cycle.size()){cout<<cycle.top()<<' ';cycle.pop();}exit(0);}}}cout<<"IMPOSSIBLE\n";}
#include<bits/stdc++.h>#define int long long#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;usingpii=pair<int,int>;constintINF=2e18;constintmaxn=3e5+5;constintM=1e9+7;intn,k,fg;vector<int>G[maxn];inta[maxn],dis[maxn],instk[maxn];voiddfs(intu){instk[u]=true;for(autov:G[u]){if(dis[v]==0){dis[v]=dis[u]+1;dfs(v);}elseif(instk[v]){if(dis[u]-dis[v]+1!=k){fg=false;}}}instk[u]=false;}voidsolve(){cin>>n>>k;fg=true;for(inti=1;i<=n;i++){dis[i]=0;instk[i]=0;G[i].clear();}for(inti=1;i<=n;i++){cin>>a[i];G[i].pb(a[i]);}if(k==1){for(inti=1;i<=n;i++){if(a[i]!=i){cout<<"NO\n";return;}}cout<<"YES\n";return;}for(inti=1;i<=n;i++){if(dis[i]==0){dis[i]=1;dfs(i);if(fg==false){cout<<"NO\n";return;}}}cout<<"YES\n";}signedmain(){// ios::sync_with_stdio(0);// cin.tie(0);intt=1;cin>>t;while(t--){solve();}}
#include<bits/stdc++.h>#define int long long#define pii pair<int, int>#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;constintINF=2e18;constintmaxn=2e6+10;constintM=1e9+7;intn,m,ans,sz;intinstk[maxn],dfn[maxn],vis[maxn],q[maxn],from[maxn],d[maxn];intcycle[maxn],sum[maxn],a[maxn];vector<int>G[maxn];voidget_cycle(intx,inty){while(y!=x){cycle[++sz]=y;sum[sz]=a[y];y=from[y];}cycle[++sz]=x;sum[sz]=a[x];for(inti=1;i<=sz;i++){vis[cycle[i]]=true;cycle[sz+i]=cycle[i];sum[sz+i]=sum[i];}for(inti=1;i<=2*sz;i++)sum[i]+=sum[i-1];}voidfind_cycle(intu,intpar){dfn[u]=instk[u]=true;for(autov:G[u]){if(v==par)continue;if(!dfn[v]){from[v]=u;find_cycle(v,u);}elseif(instk[v]){get_cycle(v,u);}}instk[u]=false;}voidcal_tree(intu){vis[u]=true;d[u]=a[u];for(autov:G[u]){if(vis[v])continue;cal_tree(v);ans=max({ans,d[u]+d[v],d[v]+a[u],a[u]});d[u]=max(d[u],d[v]+a[u]);}}voidinit(){cin>>n>>m;intu,v;for(inti=1;i<=m;i++){cin>>u>>v;G[u].pb(v);G[v].pb(u);}for(inti=1;i<=n;i++)cin>>a[i];}voidwork(){ans=-INF;if(m==n){find_cycle(1,-1);for(inti=1;i<=sz;i++)cal_tree(cycle[i]);intl=1,r=0;for(inti=1;i<=2*sz;i++){while(l<=r&&q[l]<=i-sz)l++;if(l<=r)ans=max(ans,d[cycle[i]]+d[cycle[q[l]]]+sum[i]-sum[q[l]-1]-a[cycle[i]]-a[cycle[q[l]]]);while(l<=r&&d[cycle[q[r]]]-sum[q[r]-1]-a[cycle[q[r]]]<=d[cycle[i]]-sum[i-1]-a[cycle[i]-1])r--;q[++r]=i;}}else{cal_tree(1);}cout<<ans<<'\n';}signedmain(){ios::sync_with_stdio(0);cin.tie(0);init();work();}
#include<bits/stdc++.h>#define int long long#define pii pair<int, int>#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;constintINF=2e18;constintmaxn=2e6+10;constintM=1e9+7;structEdge{intu,to,w;};intn,m,ans,sz;intinstk[maxn],dfn[maxn],vis[maxn],q[maxn],from[maxn],d[maxn];Edgeedges[maxn];intcycle[maxn],sum[maxn];vector<int>G[maxn];voidget_cycle(intx,inty,intweight){sum[1]=weight;while(y!=x){cycle[++sz]=y;sum[sz+1]=edges[from[y]].w;y=edges[from[y]^1].to;}cycle[++sz]=x;for(inti=1;i<=sz;i++){vis[cycle[i]]=true;cycle[sz+i]=cycle[i];sum[sz+i]=sum[i];}for(inti=1;i<=2*sz;i++)sum[i]+=sum[i-1];}voidfind_cycle(intu,intpre_eid){dfn[u]=instk[u]=true;for(autoeid:G[u]){if((eid^1)==pre_eid)continue;auto[u,v,w]=edges[eid];from[v]=eid;if(!dfn[v]){find_cycle(v,eid);}elseif(instk[v]){get_cycle(v,u,w);}}instk[u]=false;}voidcal_tree(intu){vis[u]=true;for(autoeid:G[u]){auto[u,v,w]=edges[eid];if(vis[v])continue;cal_tree(v);ans=max(ans,d[u]+d[v]+w);d[u]=max(d[u],d[v]+w);}}voidinit(){cin>>n;intu,v,w;m=1;for(inti=1;i<=n;i++){u=i;cin>>v>>w;edges[++m]={u,v,w};G[u].pb(m);edges[++m]={v,u,w};G[v].pb(m);}}voidwork(){intres=0;for(inti=1;i<=n;i++){if(dfn[i])continue;sz=ans=0;find_cycle(i,-1);for(inti=1;i<=sz;i++)cal_tree(cycle[i]);intl=1,r=0;for(inti=1;i<=2*sz;i++){while(l<=r&&q[l]<=i-sz)l++;if(l<=r)ans=max(ans,d[cycle[i]]+d[cycle[q[l]]]+sum[i]-sum[q[l]]);while(l<=r&&d[cycle[q[r]]]-d[cycle[i]]<=sum[q[r]]-sum[i])r--;q[++r]=i;}res+=ans;}cout<<res<<'\n';}signedmain(){ios::sync_with_stdio(0);cin.tie(0);init();work();}
#include<bits/stdc++.h>#define int long long#define pii pair<int,int>#define pb push_back#define f first#define s secondusingnamespacestd;constintINF=1e18,MAXN=1e6+100;intt,n;vector<int>a(MAXN),in(MAXN),vis(MAXN);signedmain(){ios_base::sync_with_stdio(0);cin.tie(0);cin>>t;while(t--){cin>>n;for(inti=1;i<=n;i++){in[i]=0,vis[i]=0;}for(inti=1;i<=n;i++){cin>>a[i];a[i]=i-a[i];in[a[i]]++;}queue<int>q;for(inti=1;i<=n;i++){if(in[i]==0)q.push(i);}while(q.size()){intu=q.front();q.pop();vis[u]=1;intv=a[u];if(!vis[v]){in[v]--;if(in[v]==0)q.push(v);}}intsum=0;for(inti=1;i<=n;i++){if(!vis[i])sum++;}cout<<sum<<"\n";for(inti=1;i<=n;i++){if(!vis[i])cout<<i<<" ";}cout<<"\n";}}
#include<bits/stdc++.h>usingnamespacestd;structedge{intcow;// which cow's choice intto;boolis_first;edge(){};edge(intcow,intto,boolis_first):cow(cow),to(to),is_first(is_first){};};intN,M;vector<edge>adj[100001];boolvisited_cycle[100001];// array for cycle findingboolvisited[100001];// visited array for finding which order of cows we should useboolgets_cereal[100001];inthungry_cows=0;queue<int>order;intignore_edge=-1;intfirst_cereal=-1;// the cereal we start the search from, if the CC is not a tree then this must be on a cyclevoidfind_cycle(intcur,intprev=-1){visited_cycle[cur]=true;for(edgenext:adj[cur]){if(visited_cycle[next.to]){if(first_cereal==-1&&next.to!=prev){if(next.is_first){first_cereal=next.to;}else{first_cereal=cur;}ignore_edge=next.cow;order.push(next.cow);gets_cereal[next.cow]=true;}}else{find_cycle(next.to,cur);}}}voiddfs(intcur){visited[cur]=true;for(edgenext:adj[cur]){if(!visited[next.to]&&next.cow!=ignore_edge){gets_cereal[next.cow]=true;order.push(next.cow);dfs(next.to);}}}intmain(){cin>>N>>M;for(inti=0;i<N;++i){inta,b;cin>>a>>b;adj[a].push_back(edge(i+1,b,false));adj[b].push_back(edge(i+1,a,true));}for(inti=1;i<=M;++i){first_cereal=-1;ignore_edge=-1;if(!visited[i]){find_cycle(i);if(first_cereal>0){dfs(first_cereal);}else{dfs(i);}}}for(inti=1;i<=N;++i){if(!gets_cereal[i]){++hungry_cows;order.push(i);}}cout<<hungry_cows<<endl;while(!order.empty()){cout<<order.front()<<endl;order.pop();}return0;}
#include<bits/stdc++.h>#define int long long#define pb push_back#define mk make_pair#define F first#define S secondusingnamespacestd;constintINF=2e18;constintmaxn=2e5+5;intn,q;intpar[maxn][32],vis[maxn];vector<int>G[maxn];voidinit(){cin>>n>>q;for(inti=1;i<=n;i++){cin>>par[i][0];}}// void dfs (int u) {// if (vis[u]) return;// vis[u] = true;// dfs (par[u][0]);// }intjump(intx,intd){inti=0;while(d!=0){if(d&1)x=par[x][i];d>>=1;i++;}returnx;}voidsolve(){// for (int i = 1; i <= n; i++) {// if (!vis[i]) dfs (i);// }for(intj=1;j<32;j++){for(inti=1;i<=n;i++){par[i][j]=par[par[i][j-1]][j-1];}}while(q--){intu,k;cin>>u>>k;cout<<jump(u,k)<<"\n";}}signedmain(){ios::sync_with_stdio(0);cin.tie(0);init();solve();}