【题解】Points, Lines and Ready-made Titles Codeforces 871C 图论
Prelude
真是一道好题,然而比赛的时候花了太多时间在B题上,没时间想这个了QAQ。
题目链接:萌萌哒传送门(。^▽^)
Solution
观察样例和样例解释,我们发现,假如有四个点,恰好占据在某一个矩形的四个顶点上,那么这个矩形的四条边,都是可以自由选择画或者不画的。
这时候,我们称这四个点每个点“控制”了四条直线中的一条,可以自由选择这条直线画或者不画。这里可以配合样例解释理解一下。
考虑建立常用的图论模型——行列模型,把每一行抽象成一个点,每一列抽象成一个点,假如在\((x,y)\)处有一个点,那么用一条边连接第\(x\)行和第\(y\)列所代表的点。
在这个图上,每条边可以选择“控制”她所连接的两个点中的一个。
再次观察样例,发现如果四个点恰好占据在一个矩形的四个顶点上,那么这个连通块内会有一个环。
紧接着我们发现,如果在建出来的图上,某个连通块内有一个环的话,那么这个连通块内的每一个点,都可以被一条边“控制”,并且这些边不会重复。
也就是说,这个连通块内的每一个点所代表的直线,都是可以自由选择画或者不画的,因此,假设这个连通块内的点数为\(n\),则这个连通块内的方案数为\(2^{n}\)。
假如某一个连通块内没有环,即形成了一棵树,那么容易发现,只有“所有直线都画”这一种情况没法办到,因此,方案数为\(2^{n}-1\)。
根据乘法原理,每个连通块之间互不干扰,所以把每个连通块的方案数乘起来就好了。
Code
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
#include <cctype>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 200010;
const int MOD = 1e9+7;
int read() {
int x = 0, ch;
while( isspace(ch = getchar()) );
do x = x * 10 + ch - '0';
while( isdigit(ch = getchar()) );
return x;
}
int n;
map<int,int> idx, idy;
int nid;
namespace G {
int head[MAXN], nxt[MAXN], to[MAXN], m;
void init() {
memset(head, -1, sizeof head);
}
void adde( int u, int v ) {
to[m] = v, nxt[m] = head[u], head[u] = m++;
to[m] = u, nxt[m] = head[v], head[v] = m++;
}
}
int fpow( int a, int b ) {
int c = 1;
while(b) {
if( b & 1 ) c = int(1LL * c * a % MOD);
a = int(1LL * a * a % MOD);
b >>= 1;
}
return c;
}
int vis[MAXN];
queue<int> q;
int solve( int s ) {
using namespace G;
int e = 0, o = 0; // 边数和点数
vis[s] = 1, q.push(s);
while( !q.empty() ) {
int u = q.front(); q.pop();
++o;
for( int i = head[u]; ~i; i = nxt[i], ++e )
if( !vis[to[i]] )
vis[to[i]] = 1, q.push(to[i]);
}
e >>= 1;
return e == o-1 ? (fpow(2, o) - 1 + MOD) % MOD : fpow(2, o);
}
int main() {
n = read();
G::init();
for( int i = 0; i < n; ++i ) {
int x = read(), y = read();
if( !idx[x] ) idx[x] = ++nid;
if( !idy[y] ) idy[y] = ++nid;
G::adde(idx[x], idy[y]);
}
int ans = 1;
for( int i = 1; i <= nid; ++i )
if( !vis[i] )
ans = int(1LL * ans * solve(i) % MOD);
printf( "%d\n", ans );
return 0;
}
【题解】Points, Lines and Ready-made Titles Codeforces 871C 图论的更多相关文章
- codeforces 872E. Points, Lines and Ready-made Titles
http://codeforces.com/contest/872/problem/E E. Points, Lines and Ready-made Titles time limit per te ...
- Codeforces Round #440 (Div. 1, based on Technocup 2018 Elimination Round 2) C - Points, Lines and Ready-made Titles
C - Points, Lines and Ready-made Titles 把行列看成是图上的点, 一个点(x, y)就相当于x行 向 y列建立一条边, 我们能得出如果一个联通块是一棵树方案数是2 ...
- Codeforces 870E Points, Lines and Ready-made Titles:并查集【两个属性二选一】
题目链接:http://codeforces.com/problemset/problem/870/E 题意: 给出平面坐标系上的n个点. 对于每个点,你可以画一条经过这个点的横线或竖线或什么都不画. ...
- Codeforces 870E Points, Lines and Ready-made Titles 计数
题目链接 题意 给定二维坐标上的\(n\)个点,过每个点可以 画一条水平线 或 画一条竖直线 或 什么都不画,并且若干条重合的直线被看做同一条.问共可能得到多少幅不同的画面? 题解 官方题解 仆の瞎扯 ...
- Codeforces 871C 872E Points, Lines and Ready-made Titles
题 OvO http://codeforces.com/contest/871/problem/C ( Codeforces Round #440 (Div. 1, based on Technocu ...
- R语言:多个因变量时,如何在plot函数中画多条曲线(plot,points,lines,legend函数)
最近阅读一篇文献<Regional and individual variations in the function of the human eccrine sweat gland>, ...
- Leaflet入门:添加点线面并导入GeoJSON数据|Tutorial of Leaflet: Adding Points, Lines, Polygons and Import GeoJSON File
Web GIS系列: 1.搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 2.使用GeoServer+QGIS发布WMTS服务 3.使 ...
- Ant Man CodeForces - 704B (图论,贪心)
大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的距离计算公式 |xi - xj| + ci + bj seconds if j< i |xi - xj| ...
- [题解] [NOIP2008] 双栈排序——关系的冲突至图论解法
Problem 题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操 ...
随机推荐
- zabbix_agentd-install.sh (脚本部署zabbix_agentd服务)
原文发表于cu:2016-05-20 基于http://www.cnblogs.com/netonline/p/7406598.html(http://blog.chinaunix.net/uid-2 ...
- 《Cocos2d-x游戏开发实战精解》学习笔记1--在Cocos2d中显示图像
Cocos2d-x中的图像是通过精灵类来显示的.在Cocos2d-x中游戏中的每一个角色.怪物.道具都可以理解成是一个精灵,游戏背景作为一种特殊的单位将其理解成是一个精灵也没有什么不妥.在源文件本章目 ...
- DeepLearning - Regularization
I have finished the first course in the DeepLearnin.ai series. The assignment is relatively easy, bu ...
- 微软职位内部推荐-Software Engineer II-Data Mining
微软近期Open的职位: Are you looking for a big challenge? Do you know why Big Data is the next frontier for ...
- Kickstart 安装centos7
以前是怎么安装系统的 光盘(ISO文件,光盘的镜像文件)===>每一台物理机都得给一个光驱,如果用外置光驱的话,是不是每台机器都需要插一下 U盘:ISO镜像刻录到U盘==>需要每台机器都需 ...
- Python20-Day02
1.数据 数据为什么要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同类型的数据表示: 数据类型 数字(整形,长整形,浮点型,复数),字符串,列表,元组,字典,集合 2.字符串 1.按索引取 ...
- POJ 2376 (区间问题,贪心)
题目链接:http://poj.org/problem?id=2376 题目大意:选择一些区间使得能够覆盖1-T中的每一个点,并且区间数最少 题目分析:这道题目很明显可以用贪心法来解决.但题目没有看起 ...
- net项目调试时,读取主干或其他项目代码问题
最近调试项目的时候出了一个很奇怪的问题. 项目总是去读取另外一个项目的配置文件,甚至执行的代码都是另外一个项目的. 我用vs修改代码时候,甚至修改到了其他项目的代码,生成不报错,很奇怪. 本来怀疑是v ...
- c语言----程序记录
1.结构体写入文件,读取 #include <stdio.h> #include <string.h> #include <stdlib.h> #define ma ...
- 新浪 ip 地址库
API地址:http://int.dpool.sina.com.cn/iplookup/iplookup.php 帮助 1 2 3 4 5 6 7 8 function get_location($i ...