UVA 10319,uva10319

來源:互聯網
上載者:User

UVA 10319,uva10319
UVA 10319 - Manhattan

題目連結

題意:一個城市,有南北和東西街道,每種街道都是單行道,現在給定幾個起點和終點,要求起點和終點必須最多轉一次彎可以到達,問是否可以滿足所有的起點終點

思路:2-set,對於東西,南北街道,向西(北)為false,向東(南)為true,對於每個起點終點,肯定是走座標組應的那4條街道,表示出來是(s1 & a2) | (s2 & a1),可以轉化成(s1 | s2) & (s1 | a1) & (a2 | v2) & (a2 | a1)對應2-set4條邊,注意如果橫座標或縱座標相同,只要建一條邊(s, s)即可,這樣建圖,進行2-set判定即可

代碼:

#include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <algorithm>using namespace std;const int MAXNODE = 2005;struct TwoSet {int n;vector<int> g[MAXNODE * 2];bool mark[MAXNODE * 2];int S[MAXNODE * 2], sn;void init(int tot) {n = tot * 2;for (int i = 0; i < n; i += 2) {g[i].clear();g[i^1].clear();}memset(mark, false, sizeof(mark));}void add_Edge(int u, int uval, int v, int vval) {u = u * 2 + uval;v = v * 2 + vval;g[u^1].push_back(v);g[v^1].push_back(u);}bool dfs(int u) {if (mark[u^1]) return false;if (mark[u]) return true;mark[u] = true;S[sn++] = u;for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (!dfs(v)) return false;}return true;}bool solve() {for (int i = 0; i < n; i += 2) {if (!mark[i] && !mark[i + 1]) {sn = 0;if (!dfs(i)){for (int j = 0; j < sn; j++)mark[S[j]] = false;sn = 0;if (!dfs(i + 1)) return false;}}}return true;}} gao;int n, S, A, m;int main() {scanf("%d", &n);while (n--) {scanf("%d%d%d", &S, &A, &m);gao.init(S + A);int s1, a1, s2, a2;while (m--) {scanf("%d%d%d%d", &s1, &a1, &s2, &a2);s1--; a1--; s2--; a2--;if (s1 == s2 && a1 == a2) continue;a1 += S;a2 += S;if (s1 == s2) gao.add_Edge(s1, (a1 < a2), s2, (a1 < a2));else if (a1 == a2) gao.add_Edge(a1, (s1 < s2), a2, (s1 < s2));else {gao.add_Edge(s1, (a1 < a2), s2, (a1 < a2));gao.add_Edge(s1, (a1 < a2), a1, (s1 < s2));gao.add_Edge(a2, (s1 < s2), s2, (a1 < a2));gao.add_Edge(a2, (s1 < s2), a1, (s1 < s2));}}printf("%s\n", gao.solve() ? "Yes" : "No");}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.