当前位置:网站首页>J. Serval and essay (tarjan finds topological order)
J. Serval and essay (tarjan finds topological order)
2022-07-24 01:45:00 【I want to eat egg yolk and meat dumplings】
nature :tarjan Every time push_back(s[tp]) Then we can get the reverse topological order without shrinking points .
Portal :J.Serval and Essay
The question : Here's a picture for you , You can select any starting point st(st In degree can not be 0). from u arrive v after ,u Will be deleted .u Able to reach v If and only if v The degree of entry is 1( arrive v after v The degree of entry is 0), Ask how many points you can reach at most .
analysis : Not all in a ring can be taken , No way to shrink . consider tarjan Enumerate the starting point after finding the topological order .
Code :
#include<bits/stdc++.h>
using namespace std;
#define int long long
// #define int __int128
#define pii pair<int,int>
#define endl '\n'// Interactive questions need endl Refresh //
const int maxn=1e6+5;
vector<int>e[maxn];
int x[maxn],y[maxn];
int dfn[maxn],low[maxn],s[maxn],instk[maxn],tp;
int scc[maxn],sc;// node i Where scc Number
int sz[maxn];//scc size
int dfncnt;
int ind[maxn];
int vis[maxn];
int ct;
vector<int>ord;
void tarjan(int u)
{
low[u]=dfn[u]=++dfncnt,s[++tp]=u,instk[u]=1;
for(auto v:e[u])
{
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instk[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
++sc;
while(1)
{
scc[s[tp]]=sc;
sz[sc]++;
ord.push_back(s[tp]);// Record the reverse topological order of non shrinking points
instk[s[tp]]=0;
--tp;
if(s[tp+1]==u) break;
}
}
}
void solve()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) e[i].clear(),sz[i]=dfn[i]=ind[i]=vis[i]=0;
dfncnt=sc=tp=0;
ord.clear();
for(int i=1;i<=n;i++)
{
int k;
cin>>k;
for(int j=1;j<=k;j++)
{
int x;
cin>>x;
e[x].push_back(i);
ind[i]++;
}
}
for(int i=1;i<=n;i++)
{
if(!dfn[i]) tarjan(i);
}
reverse(ord.begin(),ord.end());// After flipping, it is a topological order without shrinking points
int maxx=0;
for(auto st:ord)// Enumeration starting point
{
if(vis[st]) continue;
vector<int>q{st},ops;
for(int i=0;i<q.size();i++)// Each recalculation q.size(), And auto Different
{
int u=q[i];
vis[u]=1;
for(auto v:e[u])
{
if(v==st) continue;// Ring
ops.push_back(v);
if(--ind[v]==0) q.push_back(v);
}
}
maxx=max(maxx,(int)q.size());// from st set out , At most q.size() Point penetration is 0( You count st)
for(auto v:ops) ind[v]++;
}
printf("Case #%lld: %lld\n",++ct,maxx);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
cin>>t;
while(t--) solve();
}边栏推荐
- Vantui, axiso, FAQs and usage:
- Exchange 2013 SSL certificate installation document
- Summary of volatile interview in concurrent programming
- Disadvantages of win11
- Introduction to digital signature technology
- Jenkins multitask concurrent construction
- Research on retinal vascular segmentation based on GAN using few samples
- Database design
- Hcip network type, PPP session, data link layer protocol
- Review of HCIA
猜你喜欢

Hcip first day notes

Notes - record the solution to the failure of @refreshscope dynamic refresh configuration

Spark memory management mechanism new version

Hardware knowledge 2 -- Protocol class (based on Baiwen hardware operation Daquan video tutorial)
![[code case] website confession wall & to do list (including complete source code)](/img/90/c98295ce16551c775380ad6a912956.png)
[code case] website confession wall & to do list (including complete source code)

Thread pool interview

Non boost ASIO notes: UDP UART socketcan multicast UDS

Measurement and acquisition of permanent magnet motor parameters (inductance, resistance, pole number, flux linkage constant)

Mysql database authorization learning

Number of combinations....
随机推荐
Ora-12899 error caused by nchar character
Kotlin foundation from introduction to advanced series explanation (basic chapter) keyword: suspend
Construction and test of hfish honey pot
Retinal network based on enhanced spatial attention (ESA UNET)
Hcip day 4 notes
Basic knowledge of mathematical vector
Arm architecture and programming 5 -- GCC and makefile (based on Baiwen arm architecture and programming tutorial video)
复制可读路径不好使
简单GAN实例代码
Database paradigm and schema decomposition
Matplotlib save image to file
Simple Gan instance code
Code reading methods and best practices
Advantages and disadvantages of XML
How to use the directory classification function of the new version of easycvr (v2.5.0)?
Basic network solutions for small and medium-sized hospitals
EFCore高级Saas系统下一个DbContext如何支持多数据库迁移
SCM learning notes 6 -- interrupt system (based on Baiwen STM32F103 series tutorials)
医院综合布线
Hcip day 5 notes