Wormholes

Farmer John's hobby of conducting high-energy physics experiments on weekends has backfired, causing N wormholes (2 <= N <= 12, N even) to materialize on his farm, each located at a distinct point on the 2D map of his farm (the x,y coordinates are both integers).

According to his calculations, Farmer John knows that his wormholes will form N/2 connected pairs. For example, if wormholes A and B are connected as a pair, then any object entering wormhole A will exit wormhole B moving in the same direction, and any object entering wormhole B will similarly exit from wormhole A moving in the same direction. This can have rather unpleasant consequences.

For example, suppose there are two paired wormholes A at (1,1) and B at (3,1), and that Bessie the cow starts from position (2,1) moving in the +x direction. Bessie will enter wormhole B [at (3,1)], exit from A [at (1,1)], then enter B again, and so on, getting trapped in an infinite cycle!

   | . . . .
| A > B . Bessie will travel to B then
+ . . . . A then across to B again

Farmer John knows the exact location of each wormhole on his farm. He knows that Bessie the cow always walks in the +x direction, although he does not remember where Bessie is currently located.

Please help Farmer John count the number of distinct pairings of the wormholes such that Bessie could possibly get trapped in an infinite cycle if she starts from an unlucky position. FJ doesn't know which wormhole pairs with any other wormhole, so find all the possibilities.

PROGRAM NAME: wormhole

INPUT FORMAT:

Line 1: The number of wormholes, N.
Lines 2..1+N: Each line contains two space-separated integers describing the (x,y) coordinates of a single wormhole. Each coordinate is in the range 0..1,000,000,000.

SAMPLE INPUT (file wormhole.in):

4
0 0
1 0
1 1
0 1

INPUT DETAILS:

There are 4 wormholes, forming the corners of a square.

OUTPUT FORMAT:

Line 1: The number of distinct pairings of wormholes such that Bessie could conceivably get stuck in a cycle walking from some starting point in the +x direction.

SAMPLE OUTPUT (file wormhole.out):

2

OUTPUT DETAILS:

If we number the wormholes 1..4 as we read them from the input, then if wormhole 1 pairs with wormhole 2 and wormhole 3 pairs with wormhole 4, Bessie can get stuck if she starts anywhere between (0,0) and (1,0) or between (0,1) and (1,1).

   | . . . .
4 3 . . . Bessie will travel to B then
1-2-.-.-. A then across to B again

Similarly, with the same starting points, Bessie can get stuck in a cycle if the pairings are 1-3 and 2-4 (if Bessie enters WH#3 and comes out at WH#1, she then walks to WH#2 which transports here to WH#4 which directs her towards WH#3 again for a cycle).

Only the pairings 1-4 and 2-3 allow Bessie to walk in the +x direction from any point in the 2D plane with no danger of cycling.

————————————————————————————题解

为了提醒一下自己错了那么傻【哔——】的一个错误

题目大意是一个奇怪的农夫炸出了许多黑洞【他怎么办到的……】黑洞两两联通,假如1,2黑洞是一对,从1黑洞进去会从2黑洞出来,从2黑洞进去会从1黑洞出来,然后他蠢得要死的奶牛不会躲,而且傻乎乎地往x正半轴的方向走,他的奶牛可能进入死循环,然后就gg了……然后这个炸出黑洞的人并不知道哪两个黑洞联通,我们要计算这些黑洞会在任意一点出现死循环的配对数

我们离散化一下,然后暴力搭配,然后在任意一个黑洞模拟走路就可以了,就是最后一个模拟没写好,挂了三次……

 /*
PROB: wormhole
LANG: C++
ID: jiaqi si
*/
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#define ivory
#define mo 1000000007
#define siji(i,x,y) for(int i=(x);i<=(y);i++)
#define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
#define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
#define pii pair<int,int>
#define fi first
#define se second
#define mo 1000000007
using namespace std;
pii a[];
int n,pline[];
int paring[],now;
bool use[];
vector<int> v[];
int ans;
bool check(int k,int prev) {
if(prev!=paring[k]) { return check(paring[k],k);}
//只要它过黑洞的时候不走回去就行,之前打了标记,然而有些时候黑洞可以走两次
else {
int s=v[pline[k]].size();
xiaosiji(i,,s) {
if(a[v[pline[k]][i]].fi==a[k].fi) {
if(i==s-) return true;
if(v[pline[k]][i+]==now) return false;
return check(v[pline[k]][i+],k);
break;
}
}
}
return true;
}
void calculate() {
siji(i,,n) {
now=i;
if(!check(i,)) {++ans;return;}
} }
void dfs(int u,int t) {
if(t==n-) {
siji(i,,n) if(!use[i]) {paring[u]=i;paring[i]=u;}
calculate();return;
}
use[u]=;
siji(i,,n) {
if(!use[i]) {
use[i]=;paring[i]=u;paring[u]=i;
siji(j,,n) {
if(!use[j]) {dfs(j,t+);break;}
}
use[i]=;
}
}
use[u]=;
}
int main()
{
#ifdef ivory
freopen("wormhole.in","r",stdin);
freopen("wormhole.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
scanf("%d",&n);
siji(i,,n) {
scanf("%d%d",&a[i].fi,&a[i].se);
swap(a[i].fi,a[i].se);
}
sort(a+,a+n+);
int it=a[].fi,cnt=;
siji(i,,n) {
if(a[i].fi==it) {v[cnt].push_back(i);pline[i]=cnt;}
else {it=a[i].fi;++cnt;v[cnt].push_back(i);pline[i]=cnt;}
swap(a[i].fi,a[i].se);
}
dfs(,);
printf("%d\n",ans);
return ;
}

最新文章

  1. Replace conditional with Polymorphism
  2. 【CityHunter】游戏进度总控,及需求设计
  3. Spring AOP AspectJ Pointcut Expressions With Examples--转
  4. Unity3D着色器Shader编程入门(一)
  5. git常用操作
  6. erlang和java通信
  7. Javascript 中的小括号 “()” 的多义性
  8. BootStrap2学习日记21---消息提示
  9. PHP学习心得(九)——函数
  10. VirtualDub - 开源视频捕捉及线性处理软件
  11. QTcpSocket 及 TCP粘包分析
  12. nginx 通过rsyslog发日志 rsyslog服务器挂掉 日志丢失问题
  13. (二)Hololens Unity 开发之 语音识别
  14. 高并发下的Java数据结构(List、Set、Map、Queue)
  15. Digital Square(hdu4394)搜索
  16. Session in BSU CodeForces - 1027F(思维 树 基环树 离散化)
  17. H2内嵌数据库使用步骤
  18. mdadm 创建md 删除md步骤
  19. Storm系列三: Storm消息可靠性保障
  20. 【微信小程序】:评论、回复和删除功能 -- 2017/7/14

热门文章

  1. [ios3-地图] 如何在iOS地图上高效的显示大量数据 [转]
  2. MVC源码分析 - Controller创建和创建扩展
  3. 正则表达式之js检验密码强度
  4. angularJS 自定义元素和属性
  5. 利用python3.5 构建流媒体后台音视频切换的服务端程序
  6. 一点公益二码公益开发模式系统源码App
  7. php常量作用
  8. JSON 和 JSONP 两兄弟
  9. November 12th 2016 Week 46th Saturday
  10. .net core 11