Codeforeces
来源:互联网 发布:如何开发一门编程语言 编辑:程序博客网 时间:2024/05/22 06:56
E. Bipartite Segments
分类:
data structures
dfs and similar
题意: 给你一个无向图,
解题思路: 判断一个无向图是否是二分图的重要性质就是——没有奇数度的环!因此,这样考虑,预处理出所有环(因为已经保证了没有偶数度环)的最小编号和最大编号,这个做法很多,类似tarjan
的方法是考虑用栈存一下当前的访问的编号,当访问到已经标记的点,则一直退栈同时更新最大值最小值(因为环内元素就是一个双连通分量!),对于每个查询,如果它不包含任何奇数度环,那么答案就是
代码:
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 3e5 + 10;vector<int> g[maxn];int vis[maxn];stack<int> S;int p[maxn];ll sum[maxn];void dfs(int u, int fa) { S.push(u); vis[u] = 1; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v != fa) { if (vis[v] == 0) dfs(v, u); else if (vis[v] == 1) { int Max = u, Min = u; while (!S.empty()) { int w = S.top(); S.pop(); Max = max(Max, w); Min = min(Min, w); if (w == v) break; }// printf("<%d %d\n", Min, Max); p[Min] = Max; } } } if (!S.empty() && S.top() == u) S.pop(); vis[u] = 2;}int main() { int n, m, q; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); g[a].push_back(b); g[b].push_back(a); } memset(vis, 0, sizeof vis); for (int i = 1; i <= n + 1; i++) p[i] = n + 1; for (int i = 1; i <= n; i++) { if (vis[i] == 0) dfs(i, -1); } sum[n + 1] = 0; for (int i = n; i >= 1; i--) { p[i] = min(p[i], p[i + 1]); sum[i] = p[i] - i + sum[i + 1];// printf("p[%d] %d - sum[%d] %I64d\n", i, p[i], i, sum[i]); } scanf("%d", &q); while (q--) { int l, r; scanf("%d%d", &l, &r); int L = l, R = r, P = l; while (L <= R) { int m = L + R >> 1; if (p[m] <= r) L = m + 1; else { P = m; R = m - 1; } } printf("%I64d\n", sum[l] - sum[P] + 1LL * (r - P + 2) * (r - P + 1) / 2); } return 0;}
- Codeforeces
- codeforeces 239B
- CodeForeces 35D
- 【Codeforeces】【#259】【Div.2】
- codeforeces 543D
- CodeForeces 25E (kmp)
- codeforeces Round #441B
- CodeForeces 665C Simple Strings
- Codeforeces 703C 躲避多边形
- codeforeces 344 E Read Time
- Codeforeces 501B map的入门
- Codeforeces 342E 对操作分块
- Codeforeces - 723D -Lakes in Berland
- CodeForeces 303C——Minimum Modular
- Codeforeces 849A Odds and Ends
- Codeforeces 453A Little Pony and Expected Maximum
- 【CodeForeces】【#290_(div.2)_C】拓扑排序
- Codeforeces 727 E 字符串双Hash 2017/1/12
- wxWidgets学习
- pip命令使用简介
- redis源码分析(七)、redis命令学习总结—Redis 有序集合(sorted set)
- php 数组去重
- 单链表插入排序
- Codeforeces
- 突破内存的桎梏——移动端纹理压缩应用与分析
- 关于在linux上你必须要学的常用命令
- xml序列化反序列化辅助类
- redis源码分析(八)、redis数据结构之压缩ziplist--------ziplist.c ziplist.h学习笔记
- ie6,7,8兼容background-size方法
- spring session +cluster redis(spring-session-data-redis)
- wampsever配置虚拟主机
- struts2配置中<action >的method{数字}属性