結果

問題 No.483 マッチ並べ
ユーザー btk
提出日時 2017-02-11 11:23:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,509 bytes
コンパイル時間 2,190 ms
コンパイル使用メモリ 188,072 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-29 12:08:03
合計ジャッジ時間 3,689 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

struct cww{cww(){
    ios::sync_with_stdio(false);cin.tie(0);
    cout<<fixed;
    cout<<setprecision(10);
}}star;
#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)

#define fi first
#define se second
#define pb push_back

template <typename T>inline void chmin(T &l,T r){l=min(l,r);}
template <typename T>inline void chmax(T &l,T r){l=max(l,r);}

template <typename T>
istream& operator>>(istream &is,vector<T> &v){
    for(auto &it:v)is>>it;
    return is;
}

#include <bits/stdc++.h>
using namespace std;

struct edge{
    int flow,to,rev;
};
typedef vector<edge> E;
typedef vector<E> Graph;

void addedge(Graph&g,int from,int to,int f){
    int x=g[from].size();
    int y=g[to].size();
    g[from].pb(edge{f,to,y});
    g[to].pb(edge{0,from,x});
}
vector<int> bfs(Graph& G,int s){
    int N = G.size();
    queue<int> que;
    vector<int> dist(N,-1);
    dist[s] = 0;
    que.push(s);
    for(;!que.empty();que.pop()){
        auto v=que.front();
        for(auto& e : G[v])
            if(e.flow>0&&dist[e.to]==-1){
                dist[e.to] = dist[v] + 1;
                que.push(e.to);
        }
    }
    return dist;
}

int maxflow(Graph& G,int s,int t){
    int res=0;
    int N = G.size();
    while(true){
        auto dist = bfs(G,s);
        if(dist[t] < 0)break;
        vector<unsigned> iter(N,0);
        std::function<int(int,int)> dfs=[&](int v,int f){
            if(v==s)return f;
            for(auto &i = iter[v]; i < G[v].size(); i++){
                edge &e = G[v][i];
                edge &re = G[e.to][e.rev];
                if(re.flow>0 && dist[v] >dist[e.to]){
                    int d=dfs(e.to,min(f,re.flow));
                    if(d>0){e.flow+=d;re.flow-=d;return d;}
                }
            }
            return 0;
        };
        int f;
        while((f=dfs(t,114514))> 0)res+=f;
    }
    return res;
}


typedef pair<int,int> P;
int main(){
    int N;
    cin>>N;
    vector<P> a(N),b(N);
    map<P,int> c;
    REP(i,N){
        cin>>a[i].fi>>a[i].se>>b[i].fi>>b[i].se;
        c[a[i]]=0;
        c[b[i]]=0;
    }
    int M=0;
    for(auto &it:c)it.se=M++;
    int S=N+M;
    int G=S+1;
    Graph g(N+M+2);
    REP(i,N)addedge(g,S,i,1);
    REP(i,M)addedge(g,N+i,G,1);
    REP(i,N){
        addedge(g,i,N+c[a[i]],1);
        addedge(g,i,N+c[b[i]],1);
    }
    if(maxflow(g,S,G)==N)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}
0