CF796D Police Stations 题解

CF796D Police Stations 题解

CF796D Police Stations 题解

思路

其实这道题就是一个染色,因为题目保证最近的警察局一定距离小于等于 d d d,所以我们可以直接忽略 d d d,直接染色,然后记录有多少条边的 u u u v v v 颜色不相同,就是答案。

代码

#include<bits/stdc++.h>
#define rep(i,s1,s2,s3) for(i = s1;i <= s2;i += s3)
#define r(i,s1,s2,s3) for(i = s1;i >= s2;i -= s3)
#define ull unsigned ll
#define INF 0x7f7f7f7f
#define ll long long
using namespace std;
int n,m,d,id,ans,col[300010],head[300010];
struct edge{
    int u,v,next;
}e[600010];
void add(int u,int v){
    e[++id] = edge{u,v,head[u]};
    head[u] = id;
}
struct node{
    int u,col;
};
queue <node> q;
int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef ONLINE_JUDGE
#else
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
    cin>>n>>m>>d;
    int i,u,v,c;
    rep(i,1,m,1){
        cin>>u;
        q.push({u,i});
    }
    rep(i,2,n,1){
        cin>>u>>v;
        add(u,v);
        add(v,u);
    }
    while(q.size()){
        u = q.front().u;
        c = q.front().col;
        q.pop();
        if(col[u]) continue;
        col[u] = c;
        for(i = head[u];i;i = e[i].next){
            v = e[i].v;
            q.push(node{v,c});
        }
    }
    rep(i,1,id,2) ans += (col[e[i].u]  != col[e[i].v]);
    cout<<ans<<'\n';
    rep(i,1,id,2) if(col[e[i].u] != col[e[i].v]) cout<<(i + 1 >> 1)<<' ';
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值