2017 Mid Central Regional G. Faulty Robot(dfs + 尬模)

这道题看上去太像tarjan缩点了,我一上去本来想把所有的环给缩掉然后统计答案,后来发现哦,这道题不是这么回事儿。

给出黑边红边,一次性走至多只能走一次黑边,问有多少个点可以走到,并且让机器人停下来,就一次这也不是很难么,但是如果k >= 2就很难说了

爆搜就完事儿了,遇到黑边改一次再走,如果一个点走不通了就统计一下,记得入点要特判

#include <bits/stdc++.h>
using namespace std;
#define limit (20000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout)
#define debug(x) cout<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
inline ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
}//快读
void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n;
int head[limit],cnt;
struct node{
    int to,next;
    bool forced;
}edge[limit];
void init(){
    memset(head, -1, sizeof(head));
    cnt = 0;
}
int ans;
void add(int x, int y,bool flag = false){
    edge[cnt].to = y;
    edge[cnt].next = head[x];
    edge[cnt].forced = flag;
    head[x] = cnt++;
}
int vis[limit],tot[limit];
void dfs(int u,  int flag){
    bool ok = false;
    traverse(u){
        int v = edge[i].to;
        bool status = edge[i].forced;
        if(status){
            if(!vis[v]){
                vis[v] = 1;
                dfs(v,flag);
                vis[v] = 0;
            }
            ok = true;
        }else if(flag){
            if(!vis[v]){
                vis[v] = 1;
                dfs(v,!flag);
                vis[v] = 1;
            }
        }
    }
    if(!ok)tot[u]= 1;
}
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    n = read();
    int m = read();
    init();
    rep(i ,1,m){
        int x = read(), y =read();
        if(x < 0){
            add(-x,y, true);
        }else{
            add(x,y);
        }
    }
    int ok = 1;
    traverse(1)if(edge[i].forced){
        ok = 0;
        break;
    }
    tot[1] = ok;
    //vis[1] = 1;;
    ans = 0;
    dfs(1,true);
    rep(i,1,n){
        ans += tot[i];
    }
    write(ans);
    return 0;
}

 

posted @ 2020-08-28 14:00  tiany7  阅读(166)  评论(0编辑  收藏  举报