P2835 burn a CD

Verbatim https://www.dazhuanlan.com/2019/08/25/5d623a5f73d79/


God tm fast reading can timed out, really amazing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

using namespace std;
const int maxn=20000;
int n,num=0,t_num=0,tot=0,ans=0;
int insta[maxn],head[maxn],dfn[maxn],low[maxn],color[maxn],du[maxn];
stack<int> s;


int x=0,t=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')t=-1;ch=getchar();}
while(ch<='9'&&ch>='0') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*t;
}*/
struct {
int v,next;
}e[maxn];
void add(int u,int v)
{
e[++num].v=v;
e[num].next=head[u];
head[u]=num;
}
void tarjan(int x)
{
dfn[x]=low[x]=++tot;
s.push(x);insta[x]=1;
for(int i=head[x];i;i=e[i].next)
{
int v=e[i].v;
if(!dfn[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(insta[v]) low[x]=min(low[x],dfn[v]);
}
int t;
if(dfn[x]==low[x])
{
t_num++;
do
{
t=s.top();
insta[t]=0;
color[t]=t_num;
s.pop();
}while(x!=t);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int p;
scanf("%d",&p);
while(p)
{
add(i,p);
scanf("%d",&p);
}
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++)
for(int j=head[i];j;j=e[j].next)
{
int v=e[j].v;
if(color[i]!=color[v]) du[color[v]]++;
}
for(int i=1;i<=t_num;i++)
if(!du[i]) ans++;
printf("%dn",ans);
return 0;
}

Guess you like

Origin www.cnblogs.com/petewell/p/11408217.html