POJ3207 Ikki's Story IV - Panda's Trick

來源:互聯網
上載者:User

題意:圓,邊上順時針放著n(n <= 1000)個點,要連m(m <= 500)條邊,每條邊可以在圓外連,可以在圓內連,不能相交,問是否可行。

分析:

這不就是個裸的2-SAT嗎,第一次寫2-SAT,寫發部落格。

首先拆點,每條邊拆成圓內連和圓外連兩個點。

1.構圖(若取i必須取j,則在i, j連邊)

2.用tarjan求強連通分量。

3.若某一對點在同一個強連通分量裡,則無解,否則有解。

4.若需要輸出方案,縮點後,建反邊,拓撲排序,每次找到能夠取出的零出度點i,並用dfs把與i同一對的點i'和i'的前驅設為不能取。(POJ3683)

#include <cstdio>#include <algorithm>using namespace std;const int N = 2005, M = 505, E = N*N;int n,m,e,tm,tp,tmp,cnt,x[M],y[M],hd[N],nxt[E],to[E],v[N],st[N],low[N],dfn[N],bl[N];void add(int x, int y) {to[++e] = y, nxt[e] = hd[x], hd[x] = e;}void tj(int x) {dfn[x] = low[x] = ++tm, st[++tp] = x, v[x] = 1;for(int i = hd[x]; i; i = nxt[i])if(!dfn[to[i]]) tj(to[i]), low[x] = min(low[x], low[to[i]]);else if(v[x] == 1) low[x] = min(low[x], dfn[to[i]]);if(low[x] == dfn[x]) {cnt++;do v[tmp=st[tp--]] = 0, bl[tmp] = cnt; while(tmp != x);}}int main() {scanf("%d%d", &n, &m);for(int i = 1; i <= m; i++) {scanf("%d%d", &x[i], &y[i]);if(x[i] > y[i]) swap(x[i], y[i]);}for(int i = 1; i < m; i++)for(int j = i+1; j <= m; j++)if((x[i]<x[j]&&y[i]>x[j]&&y[i]<y[j])||(x[j]<x[i]&&y[j]>x[i]&&y[j]<y[i]))add(i, j+m), add(j+m, i), add(i+m, j), add(j, i+m);for(int i = 1; i <= m*2; i++) if(!dfn[i]) tj(i);for(int i = 1; i <= m; i++)if(bl[i] == bl[i+m]) {puts("the evil panda is lying again");return 0;}puts("panda is telling the truth...");return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.