轰炸

来源:互联网 发布:广州万维软件 编辑:程序博客网 时间:2024/04/26 13:34

【问题描述】
战狂也在玩《魔方王国》。他只会征兵而不会建城市,因此他决定对小奇的城市进行轰炸。
小奇有n 座城市,城市之间建立了m 条有向的地下通道。战狂会发起若干轮轰炸,每轮可以轰炸任意多个城市。
每座城市里都有战狂部署的间谍,在城市遭遇轰炸时,它们会通过地下通道撤离至其它城市。非常不幸的是,在地道里无法得知其它城市是否被轰炸,如果存在两个不同的城市i,j,它们在同一轮被轰炸,并且可以通过地道从城市i 到达城市j,那么城市i 的间谍可能因为撤离到城市j 而被炸死。为了避免这一情况,战狂不会在同一轮轰炸城市i 和城市j。
你需要求出战狂最少需要多少轮可以对每座城市都进行至少一次轰炸。

【输入文件】

第一行两个整数n,m。接下来m 行每行两个整数a,b 表示一条从a 连向b的单向边。

【输出文件】

输出一行仅一个整数表示答案。
【输入样例】
5 4
1 2
2 3
3 1
4 5
【输出样例】
3
【数据规模和约定】
对于20%的数据,n,m<=10。
对于40%的数据,n,m<=1000。
对于另外30%的数据,保证无环。
100%的数据,n,m<=1000000。

缩点,最长链长度即为答案

非递归

#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<queue>#include<algorithm>#define ll long long#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)#define et(i,x) for(int i=last[x];i;i=e[i].p)#define maxx(a,b) a=max(a,b);#define minn(a,b) a=min(a,b);#define fil(a,x) memset(a,x,sizeof(a));#define N 1000010using namespace std;int read(){    int x=0,f=1;char ch=getchar();    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}void write(int x){    if(x<0) putchar('-'),x=-x;    if(x>9) write(x/10);    putchar(x%10+'0');}int n,m,st[N],t,stack[N],top=0,dfn[N],low[N],pos[N],ind[N],now=0,last[N],cnt=0,bel[N],size[N],num,a[N],ans=0,f[N];bool inst[N],vis[N];struct edge{    int f,t,p;}e[N*2];inline void add(int x,int y) {    e[++cnt].f=x;    e[cnt].t=y;    e[cnt].p=last[x];    last[x]=cnt;}void init(){    n=read(),m=read();    fo(i,1,m)    {        int x=read(),y=read();        add(x,y);    }}void tarjan(int x){    st[t=1]=x;    while(t)    {        int u=st[t],start=last[u];        if(pos[t])        {            start=pos[t],minn(low[u],low[e[start].t]);        }        else        {            stack[++top]=u;            low[u]=dfn[u]=++now;            inst[u]=1;        }        bool b=0;        for(int i=start;i;i=e[i].p)        {            int v=e[i].t;            if(!dfn[v])            {                pos[t]=i;                st[++t]=v;                b=1;                break;            }            else if(inst[v]) minn(low[u],dfn[v]);        }        if(b) continue;        if(low[u]==dfn[u])        {            num++;            stack[top+1]=0;            for(;stack[top+1]!=u;top--) inst[stack[top]]=0,bel[stack[top]]=num,size[num]++;        }        pos[t--]=0;    }}queue<int>q;void toposort(){    a[0]=0;    fo(i,1,num) if(!ind[i]) q.push(i);    while(!q.empty())    {        int u=q.front();        q.pop();        a[++a[0]]=u;        et(i,u)        {            ind[e[i].t]--;            if(!ind[e[i].t]) q.push(e[i].t);        }    }}int main(){    init();    fo(i,1,n) if(!dfn[i]) tarjan(i);        fo(i,1,n) last[i]=0;    fo(i,1,m)    {        int x=e[i].f,y=e[i].t;        if(bel[x]!=bel[y]) add(bel[x],bel[y]),ind[bel[y]]++;    }    toposort();    fo(i,1,num)    {        int x=a[i];        maxx(ans,f[x]+=size[x])        et(j,x) maxx(f[e[j].t],f[x]);    }       write(ans);    return 0;}

递归 (转载)

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int N=1000005;int n,m,cnt,last[N],bel[N],dfn[N],low[N],stack[N],top,size[N],f[N],a[N],tim,d[N],tot;bool ins[N];struct edge{int from,to,next;}e[N*2];queue<int> que;int read(){    int x=0,f=1;char ch=getchar();    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}void addedge(int u,int v){    e[++cnt].from=u;e[cnt].to=v;e[cnt].next=last[u];last[u]=cnt;}void tarjan(int x){    low[x]=dfn[x]=++top;    stack[++top]=x;ins[x]=1;    for (int i=last[x];i;i=e[i].next)        if (!dfn[e[i].to]) tarjan(e[i].to),low[x]=min(low[x],low[e[i].to]);        else if (ins[e[i].to]) low[x]=min(low[x],dfn[e[i].to]);    if (dfn[x]==low[x])    {        tot++;int y=0;        while (y!=x)        {            y=stack[top];top--;            bel[y]=tot;ins[y]=0;            size[tot]++;        }    }}void topusort(){    for (int i=1;i<=tot;i++) if (!d[i]) que.push(i);    int a1=0;    while (!que.empty())    {        int u=que.front();que.pop();a[++a1]=u;        for (int i=last[u];i;i=e[i].next)        {            d[e[i].to]--;            if (!d[e[i].to]) que.push(e[i].to);        }    }}int main(){    freopen("bomb.in","r",stdin);    freopen("bomb.out","w",stdout);    n=read();m=read();    for (int i=1;i<=m;i++)    {        int x=read(),y=read();        addedge(x,y);    }    for (int i=1;i<=n;i++) if (!dfn[i]) tarjan(i);    for (int i=1;i<=n;i++) last[i]=0;    for (int i=1;i<=m;i++)    {        int x=e[i].from,y=e[i].to;        if (bel[x]!=bel[y]) addedge(bel[x],bel[y]),d[bel[y]]++;    }    topusort();    int ans=0;    for (int i=1;i<=tot;i++)    {        int x=a[i];f[x]+=size[x];ans=max(ans,f[x]);        for (int j=last[x];j;j=e[j].next) f[e[j].to]=max(f[e[j].to],f[x]);    }    printf("%d",ans);    fclose(stdin);fclose(stdout);    return 0;}