zoukankan      html  css  js  c++  java
  • bzoj4238 电压

    嘟嘟嘟


    这题今天我们学校模拟出了,我没做出来,搞了个暴力得了30分……


    正解现在看来挺显然的,考场上之所以没想到可能是因为第二题就卡住了吧,导致想第三题的时候有些焦躁。
    首先,一个奇环是必须要选出一条边的,而一个偶环是一定不能选出一条边的。这也就可以拿到(n = m)的部分分:一遍dfs求出环的点数(x),如果为奇数,答案就是(x)(考场上我写成了(n)……),否则是(n - x)。(因为树上的边肯定都符合,但不一定非要选)
    既然树上边都符合,就启发我们在图中先找出一个dfs树,然后判断非树边和树构成了什么环。
    怎么判断,就是看树上两点距离是奇数还是偶数,如果是奇数就是偶环,否则就是奇环。而之所以找出dfs树,是因为dfs树只存在返祖边,不存在横插边,树上的距离就简化成了两点深度之差。
    然后我们给这些被环覆盖的树边打上标记。那么答案就是被所有奇环标记且没有被任意一个偶环标记过的树边。
    特判:如果只有一个奇环,答案加1,因为本身那条返祖边也可以算。


    写的时候标记没必要用树剖,树上差分即可。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e5 + 5;
    const int maxm = 2e5 + 5;
    inline ll read()
    {
    	ll ans = 0;
    	char ch = getchar(), last = ' ';
    	while(!isdigit(ch)) last = ch, ch = getchar();
    	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    	if(last == '-') ans = -ans;
    	return ans;
    }
    inline void write(ll x)
    {
    	if(x < 0) x = -x, putchar('-');
    	if(x >= 10) write(x / 10);
    	putchar(x % 10 + '0');
    }
    
    int n, m;
    struct Edge
    {
    	int nxt, to;
    }e[maxm << 1];
    int head[maxn], ecnt = -1;
    In void addEdge(int x, int y)
    {
    	e[++ecnt] = (Edge){head[x], y};
    	head[x] = ecnt;
    }
    
    bool vis[maxn];
    int dep[maxn], fa[maxn], sum[maxn][2], tot = 0;
    In void dfs(int now)
    {
    	vis[now] = 1;
    	for(int i = head[now], v; i != -1; i = e[i].nxt)
    	{
    		v = e[i].to;
    		if(i == fa[now] || (i ^ 1) == fa[now]) continue;
    		if(vis[v])
    		{
    			if(dep[v] > dep[now]) continue;  //防止走返祖边自己的反向边
    			int dis = (dep[now] - dep[v]) & 1;
    			--sum[v][dis], ++sum[now][dis]; 
    			if(!dis) ++tot;
    		}
    		else
    		{
    			fa[v] = i;
    			dep[v] = dep[now] + 1;
    			dfs(v);
    			sum[now][0] += sum[v][0], sum[now][1] += sum[v][1];
    		}
    	}
    }
    
    int main()
    {
    	Mem(head, -1); Mem(fa, -1);
    	n = read(), m = read();
    	for(int i = 1; i <= m; ++i)
    	{
    		int x = read(), y = read();
    		addEdge(x, y), addEdge(y, x);
    	}
    	for(int i = 1; i <= n; ++i) if(!vis[i]) dfs(i);
    	int ans = 0;
    	for(int i = 1; i <= n; ++i) if(sum[i][0] == tot && !sum[i][1] && ~fa[i]) ++ans;
    	if(tot == 1) ++ans;
    	write(ans), enter; 
    	return 0;
    }
    
  • 相关阅读:
    mode
    文件操作
    深浅拷贝
    基础数据类型补充
    再谈编码 decode和encode
    Python练习题 015:一颗自由落地的球
    Python练习题 014:完数
    Python练习题 013:求解a+aa+aaa……
    Python练习题 012:字符统计
    Python练习题 011:成绩打分
  • 原文地址:https://www.cnblogs.com/mrclr/p/10444599.html
Copyright © 2011-2022 走看看