結果

問題 No.483 マッチ並べ
ユーザー ei1333333
提出日時 2017-02-11 13:07:29
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,260 bytes
コンパイル時間 1,901 ms
コンパイル使用メモリ 170,208 KB
実行使用メモリ 34,788 KB
最終ジャッジ日時 2024-12-29 12:13:15
合計ジャッジ時間 4,595 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

struct Bipartite_Matching
{
  vector< vector< int > > graph;
  vector< int > match;
  vector< bool > used;

  Bipartite_Matching(int n)
  {
    graph.resize(n);
  }

  void add_edge(int u, int v)
  {
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  bool dfs(int v)
  {
    used[v] = true;
    for(int i = 0; i < graph[v].size(); i++) {
      int u = graph[v][i], w = match[u];
      if(w == -1 || (!used[w] && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return (true);
      }
    }
    return (false);
  }

  int bipartite_matching()
  {
    int ret = 0;
    match.assign(graph.size(), -1);
    for(int i = 0; i < graph.size(); i++) {
      if(graph[i].empty()) continue;
      if(match[i] == -1) {
        used.assign(graph.size(), false);
        ret += dfs(i);
      }
    }
    return (ret);
  }
};


int main()
{
  int N, R[100];

  cin >> N;

  Bipartite_Matching match(1145141);
  for(int i = 0; i < N; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    match.add_edge(i, x1 * 100 + y1 * 10000);
    match.add_edge(i, x2 * 100 + y2 * 10000);
  }

  if(match.bipartite_matching() == N) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
0