比较简单的一道题。
因为分组必须连续,故可以直接贪心。
能加则加,不能加则不加,更新可以用类似于增广的思想去做
#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define rept(i,x) for(int i = linkk[x];i;i = e[i].n)
#define P pair<int,int>
#define Pil pair<int,ll>
#define Pli pair<ll,int>
#define Pll pair<ll,ll>
#define pb push_back
#define pc putchar
#define mp make_pair
#define file(k) memset(k,0,sizeof(k))
#define ll long long
namespace fastIO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror = 0;
inline char nc(){
static char buf[BUF_SIZE],*p1 = buf+BUF_SIZE, *pend = buf+BUF_SIZE;
if(p1 == pend){
p1 = buf; pend = buf+fread(buf, 1, BUF_SIZE, stdin);
if(pend == p1){ IOerror = 1; return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign = 0; char ch = nc(); x = 0;
for(; blank(ch); ch = nc());
if(IOerror)return;
if(ch == '-') sign = 1, ch = nc();
for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
if(sign) x = -x;
}
inline void read(ll &x){
bool sign = 0; char ch = nc(); x = 0;
for(; blank(ch); ch = nc());
if(IOerror) return;
if(ch == '-') sign = 1, ch = nc();
for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
if(sign) x = -x;
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace fastIO;
int n , m , ans;
int tmp[201000] , o;
int tmp2[201000] , o2;
bool vis[201000];
bool vis2[201000];
bool flag;
vector<int>G[201000];
void dfs(int x)
{
vis[x] = true;tmp[++o] = x;
rep(i,1,G[x].size())
if(!vis[G[x][i-1]])
dfs(G[x][i-1]);
}
int main()
{
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
read(n);read(m);
vis[1] = true;
rep(i,1,m)
{
int x,y;
read(x);read(y);
if(!vis2[x]) vis2[x] = true,tmp2[++o2] = x;
G[x].pb(y);
if(vis[x] && !vis[y]) dfs(y);
if(vis[n])
{
ans++;
rep(j,1,o) vis[tmp[j]] = false;
rep(j,1,o2) G[tmp2[j]].clear();
rep(j,1,o2) vis2[tmp2[j]] = false;
o = 0;o2 = 0;
G[x].pb(y);
if(!vis2[x]) vis2[x] = true,tmp2[++o2] = x;
if(vis[x] && !vis[y]) dfs(y);
}
}
printf("%d\n",ans+1);
return 0;
}