当前位置:网站首页>J.Serval and Essay(tarjan求拓扑序)
J.Serval and Essay(tarjan求拓扑序)
2022-07-23 02:04:00 【想吃蛋黄肉粽】
性质:tarjan每次push_back(s[tp]) 就可以得到不缩点的反向拓扑序。
题意:给你一个图,可以选定任意的起点st(st入度可以不为0)。从u到达v后,u会被删除。u能够到达v 当且仅当 v的入度为1(到达v后 v的入度为0),求最多可以到达多少个点。
分析:在一个环中的都不一定能全取,缩点不行。考虑tarjan求出拓扑序后枚举起点。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
// #define int __int128
#define pii pair<int,int>
#define endl '\n'//交互题需要endl刷新//
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;//节点i所在的scc编号
int sz[maxn];//scc大小
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]);//记录不缩点的反向拓扑序
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());//翻转完是不缩点的拓扑序
int maxx=0;
for(auto st:ord)//枚举起点
{
if(vis[st]) continue;
vector<int>q{st},ops;
for(int i=0;i<q.size();i++)//每次重新计算q.size(),与auto不同
{
int u=q[i];
vis[u]=1;
for(auto v:e[u])
{
if(v==st) continue;//环
ops.push_back(v);
if(--ind[v]==0) q.push_back(v);
}
}
maxx=max(maxx,(int)q.size());//从st出发,最多有q.size()个点入度为0(算上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();
}边栏推荐
- Server memory performance tuning
- PNA polypeptide suc ala ala Pro AAA PNA suc ala3 PNA Pyr Phe Leu PNA
- Peptide nucleic acid (PNA) coupled with membrane penetrating peptides (CCPs) (KFF) 3K to form CCPs PNA | peptide nucleic acid
- pytorch 保存和加载模型
- 来看看 VSCode 的多行编辑
- 申请炒股账户在手机开户安全吗?
- A ConvNet for the 2020s 论文阅读
- PNA peptide nucleic acid modified polypeptide z-gly-pro-pna | d-phe-pip-arg-pna | TOS Gly Pro Arg PNA
- How to preserve peptide nucleic acid | peptide nucleic acid containing azobenzene unit (n-pna) | 99Tcm labeled c-myc mRNA
- Animation demonstration of binary tree implemented by MFC
猜你喜欢

PNA specification information | soybean peroxidase labeled PNA (peptide nucleic acid, PNA)

The new regulation issued the first construction certificate, and the gold content of highway and water conservancy specialty increased

Q-Vision+Kvaser CAN/CAN FD/LIN总线解决方案

AIRIOT答疑第5期|如何使用低代码业务流引擎?

肽核酸PNA规格信息|大豆过氧化酶标记肽核酸(Peptide nucleic acid,PNA)

Accumulation of FPGA errors
![[HLS] Call of queuing function in pipeline simulation](/img/b2/1de129d35b0653e5ec3f985d614c4a.png)
[HLS] Call of queuing function in pipeline simulation

服务器内存性能调优

Verilog grammar basics HDL bits training 04

第三方依赖库 AG Grid调研分析
随机推荐
How to preserve peptide nucleic acid | peptide nucleic acid containing azobenzene unit (n-pna) | 99Tcm labeled c-myc mRNA
Canal Chapter 6
复盘:什么是B+树?你知道B+树怎么构建有序表的吗?B+树有什么特点
virtualbox NAT网络模式配置
operator=中自我赋值和异常安全问题
[secret history of bug] uint8 data exceeds the type range, output 0x0102
ADAS测试方案
抖音白天与晚上触发不同特效的Graph节点编写
1058 A+B in Hogwarts
使用HiFlow场景连接器查看每天处于地区的疫情
pna肽核酸定制服务|肽核酸钳制-PCR(PNA-PCR)|cGAPPNA多价肽核酸配体
Judge whether the two types are the same
QML(17)——读写txt文件
手把手教你在群晖中设置阿里云DDNS
MATLAB之优劣解距离法Topsis模型
1058 A+B in Hogwarts
肽核酸(PNA)偶联穿膜肽(CCPs)(KFF)3K形成CCPs-PNA|肽核酸的使用方法
1057 Stack
C语言力扣第39题之组合总和。回溯法与遍历法
PNA肽核酸修饰多肽Pro-Phe-Arg-pNA (S-2302)|Dnp-Gly-X-L-Pro-Gly-pNA