zoukankan      html  css  js  c++  java
  • [BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)

    传送门

    告诉你一些点不能到达1,由于是双向边,也就是1不能到达那些点

    那么从1开始dfs,如果当前点能到达不能到达的点,那么当前点就是损坏的。

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define N 100001
    
    int n, m, p, cnt, tot;
    int head[N], to[N << 1], next[N << 1];
    bool vis[N], flag[N];
    
    inline int read()
    {
    	int x = 0, f = 1;
    	char ch = getchar();
    	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
    	return x * f;
    }
    
    inline void add(int x, int y)
    {
    	to[cnt] = y;
    	next[cnt] = head[x];
    	head[x] = cnt++;
    }
    
    inline void dfs(int u)
    {
    	int i, v;
    	vis[u] = 1;
    	for(i = head[u]; i ^ -1; i = next[i])
    	{
    		v = to[i];
    		if(flag[v]) return;
    	}
    	for(i = head[u]; i ^ -1; i = next[i])
    	{
    		v = to[i];
    		if(!vis[v]) dfs(v);
    	}
    	tot++;
    }
    
    int main()
    {
    	int i, x, y;
    	n = read();
    	m = read();
    	p = read();
    	memset(head, -1, sizeof(head));
    	for(i = 1; i <= m; i++)
    	{
    		x = read();
    		y = read();
    		add(x, y);
    		add(y, x);
    	}
    	for(i = 1; i <= p; i++)
    	{
    		x = read();
    		flag[x] = 1;
    	}
    	dfs(1);
    	printf("%d
    ", n - tot);
    	return 0;
    }
    

      

  • 相关阅读:
    javaSE基础(六)--IO流
    javaSE基础(五)--JDBC
    javaSE基础(四)--Map集合
    javaSE基础(三)--List集合
    javaSE基础(二)
    javaSE基础(一)
    eclipse快捷键大全
    mybatis学习-基础
    工厂模式
    GC日志和jvm内存的分代
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/7508244.html
Copyright © 2011-2022 走看看