D班集训day1t2 道路分组(贪心)

这里写图片描述
这里写图片描述


比较简单的一道题。
因为分组必须连续,故可以直接贪心。
能加则加,不能加则不加,更新可以用类似于增广的思想去做

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值