bzoj1638 [Usaco2007 Mar]Cow Traffic 奶牛交通

7 7
1 3
3 4
3 5
4 6
2 3
5 6
6 7

Sample Output

4

1 4
\ / \
3 6 -- 7
/ \ /
2 5

1 3 4 6 7
1 3 5 6 7
2 3 4 6 7
2 3 5 6 7

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
struct edge{
int to,next;
}e[100010];
struct bian{
int x,y,z;
}b[100010];
bool mrk[5010];
int n,m,x,y,cnt,ans;
int way1[5010],way2[5010];
inline void insert(int u,int v)
{
e[++cnt].to=v;
}
{
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;
}
inline void dfs(int x)
{
{
way1[x]=1;
return;
}
{
if (!way1[e[i].to])dfs(e[i].to);
way1[x]+=way1[e[i].to];
}
}
inline void dfs2(int x)
{
{
way2[x]=1;
return;
}
{
if (!way2[e[i].to])dfs2(e[i].to);
way2[x]+=way2[e[i].to];
}
}
int main()
{
for (int i=1;i<=m;i++)
{
mrk[b[i].y]=1;
insert(b[i].x,b[i].y);
}
for (int i=1;i<=n;i++)
if (!mrk[i]) dfs(i);
memset(e,0,sizeof(e));
cnt=0;
for (int i=1;i<=m;i++)
insert(b[i].y,b[i].x);
dfs2(n);
for (int i=1;i<=m;i++)
{
int wx=way1[b[i].y],wy=way2[b[i].x];
ans=max(ans,wx*wy);
}
printf("%d\n",ans);
}

03-10 1173

11-09 354
10-26 524
07-21 1345
02-17 980
03-09 160
10-02 73
04-18 218
11-18 3105
10-20 9109
06-19 1万+
03-24 1万+
04-17 1万+
02-04 5470