题目大意就是给你n个数,m组关系,每组的x,y满足一个关系,然后让你求出至少要分多少组才能是的每组里面的元素都没有直接或者间接的关系。
因为可能会有环路,所以需要taijan一下点然后再求出所点之后的所有点组成的最长链。
注意缩完之后的每个点的长度即为拥有的点的个数,求最长链的时候需要用一个数组记忆一下长度,否则会超时。
Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-th message shows that the age of person si is not smaller than the age of person ti. Now we need to divide all these N people into several groups. One's age shouldn't be compared with each other in the same group, directly or indirectly. And everyone should be assigned to one and only one group. The task is to calculate the minimum number of groups that meet the requirement.
Input
There are multiple test cases. For each test case: The first line contains two integers N(1≤ N≤ 100000), M(1≤ M≤ 300000), N is the number of people, and M is is the number of messages. Then followed by M lines, each line contain two integers si and ti. There is a blank line between every two cases. Process to the end of input.
Output
For each the case, print the minimum number of groups that meet the requirement one line.
Sample Input
4 4 1 2 1 3 2 4 3 4
Sample Output
3
Hint
set1= {1}, set2= {2, 3}, set3= {4}
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 110000;
using namespace std;
struct node
{
int u, v;
int next;
} f[330000];
stack<int> q;
vector<int>g[maxn];
int low[maxn];
int dfn[maxn];
int isb[maxn];
int n, m, nums;
int dfs_clock;
int num[maxn];
int dis[maxn];
int vis[maxn];
int head[maxn];
int cnt;
int in[maxn];
int ans[maxn];
void init()
{
for(int i = 0; i <= n; i++)
{
dis[i] = 0;
vis[i] = 0;
in[i] = 0;
num[i] = 0;
low[i] = 0;
dfn[i] = 0;
ans[i] = 0;
head[i] = -1;
g[i].clear();
}
dfs_clock = 0;
cnt = 0;
nums = 0;
while(!q.empty())
q.pop();
}
void add(int u, int v)
{
f[cnt].v = v;
f[cnt].next = head[u];
head[u] = cnt++;
}
void tarjan(int u)
{
low[u] = dfn[u] = ++dfs_clock;
q.push(u);
for(int i = head[u]; i != -1; i = f[i].next)
{
int v = f[i].v;
if(!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(!num[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u])
{
++nums;
while(1)
{
int x = q.top();
q.pop();
num[x] = nums;
ans[nums]++;
if(x == u)
break;
}
}
}
void dfs(int x)
{
if(dis[x])
return;
dis[x] = ans[x];
for(int i = 0; i < (int)g[x].size(); i++)
{
int y = g[x][i];
dfs(y);
dis[x] = max(dis[x], dis[y]+ans[x]);
}
}
int main()
{
while(cin >>n>>m)
{
int u, v;
init();
for(int i = 1; i <= m; i++)
{
scanf("%d %d",&u, &v);
add(u, v);
}
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 != -1; j = f[j].next)
{
int x = num[i];
int y = num[f[j].v];
if(x == y)
continue;
g[x].push_back(y);
in[y]++;
}
}
int Max = 0;
for(int i = 1; i <= nums; i++)
{
if(in[i] == 0)
{
dfs(i);
Max = max(Max, dis[i]);
}
}
cout<<Max<<endl;
}
return 0;
}