結果

問題 No.1640 簡単な色塗り
ユーザー harurunharurun
提出日時 2021-06-17 00:59:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,759 bytes
コンパイル時間 1,091 ms
コンパイル使用メモリ 99,508 KB
実行使用メモリ 5,256 KB
最終ジャッジ日時 2023-09-12 01:30:16
合計ジャッジ時間 9,546 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 RE -
testcase_03 RE -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 WA -
testcase_17 RE -
testcase_18 RE -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 RE -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 RE -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 WA -
testcase_40 WA -
testcase_41 RE -
testcase_42 RE -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 RE -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt RE -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <atcoder/internal_queue>
#include <cassert>
#include <limits>
#include <queue>
#include <vector>
#include <time.h>
#include <chrono>
#include <sys/time.h>
#include <iostream>
#include <fstream>
using namespace std;
using namespace atcoder;
#define ll long long
#define INF 1000000
using std::cout; using std::endl;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::seconds;
using std::chrono::system_clock;

//namespace atcoder {

ostream& operator<<(ostream& os,vector<int>& a){
    for(int i:a){
        os<<i<<" ";
    }
    return os<<"\n";
}

vector<ll> ans(200010,-1);

    struct _edge {
        ll to, rev;
        ll cap;
        ll index;
    };

template <class Cap> struct mf_graph {
  public:
    mf_graph() : _n(0) {}
    mf_graph(ll n) : _n(n), g(n) {}

    ll add_edge(ll from, ll to, Cap cap,ll index) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        assert(0 <= cap);
        ll m = (long long)pos.size();
        pos.push_back({from, (long long)g[from].size()});
        ll from_id = (long long)g[from].size();
        ll to_id = (long long)g[to].size();
        if (from == to) to_id++;
        g[from].push_back(_edge{to, to_id, cap, index});
        g[to].push_back(_edge{from, from_id, 0, index});
        return m;
    }

    struct edge {
        ll from, to;
        Cap cap, flow;
    };

    edge get_edge(ll i) {
        ll m = (long long)pos.size();
        assert(0 <= i && i < m);
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
    }

    Cap flow(ll s, ll t) {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    Cap flow(ll s, ll t, Cap flow_limit) {
        assert(0 <= s && s < _n);
        assert(0 <= t && t < _n);
        assert(s != t);

        time_t flow_start=time(NULL);

        std::vector<ll> level(_n), iter(_n);
        internal::simple_queue<ll> que;

        cerr<<level.size()<<endl;

        auto bfs = [&]() {
            std::fill(level.begin(), level.end(), -1);
            level[s] = 0;
            que.clear();
            que.push(s);
            while (!que.empty()) {
                ll v = que.front();
                que.pop();
                for (auto e : g[v]) {
                    if (e.cap == 0 || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    if (e.to == t) return;
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, Cap up) {
            if (v == s) return up;
            if (time(NULL)-flow_start>1.8){
              cout<<"No\n";
              exit(0);
            }
            Cap res = 0;
            //cerr<<"size:"<<level.size()<<endl;
            ll level_v = level[v];
            for (long long& i = iter[v]; i < (long long)g[v].size(); i++) {
                //cout<<"i:"<<i<<endl;
                _edge& e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d =
                    self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                // if(e.index!=-1){
                //     ans[e.index] = e.to+1;
                //     cout<<"ans update"<<endl;
                // }
                // cout<<"v:"<<v<<" "<<i<<endl;
                // cout<<e.to<<" "<<e.rev<<" "<<e.cap<<" "<<e.index<<endl<<endl;
                res += d;
                if (res == up) break;
            }
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit) {
            bfs();
            //cout<<level<<endl;
            if (level[t] == -1) break;
            std::fill(iter.begin(), iter.end(), 0);
            while (flow < flow_limit) {
                Cap f = dfs(dfs, t, flow_limit - flow);
                if (!f) break;
                flow += f;
            }
        }
        return flow;
    }
    

  //private:
    ll _n;

    std::vector<std::pair<ll, ll>> pos;
    std::vector<std::vector<_edge>> g;    
};

ostream& operator<<(ostream& os,const _edge& a){
      return  os<<a.to<<" "<<a.rev<<" "<<a.cap<<" "<<a.index;
    }

//}  // namespace atcoder

signed main(){
  ifstream ifs("input.txt");
  auto start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
  ll N,A,B;
  //cin>>N;
  ifs>>N;
  //Dinic F(200002);
  //Dinic F;
  //cout<<"INF:"<<F.INF<<endl;
  mf_graph<ll> F(2*N+2);
  for(ll i=0;i<N;i++){
    //cerr<<i<<endl;
    //cin>>A>>B;
    ifs>>A>>B;
    A--;B--;
    /*
    F.add_edge(200000,i,1);
    F.add_edge(i,100000+A,1);
    if(A!=B)F.add_edge(i,100000+B,1);
    F.add_edge(100000+i,200001,1);
    */
    F.add_edge(2*N,i,1,-1);
    F.add_edge(i,N+A,1,i);
    if(A!=B)F.add_edge(i,N+B,1,i);
    F.add_edge(N+i,2*N+1,1,-1);
  }
  //cerr<<"edge added\n";
  //ll cnt=F.max_flow(200000,200001);
  
  ll cnt=F.flow(2*N,2*N+1);
  //cout<<cnt<<endl;
  if(cnt!=N){
    cout<<"No\n";
    return 0;
  }
  //ここから構築
  cout<<"Yes\n";
  /*
  for(int i=0;i<N;i++){
    cout<<ans[i]<<endl;
  }
  */
  for(ll i=N;i<2*N;i++){
    ll s=F.g.at(i).size();
    //cout<<i<<endl;
    for(ll j=0;j<s;j++){
      if(F.g.at(i).at(j).cap!=0){
        ans.at(F.g.at(i).at(j).to)=i-N+1;
      }
      //cout<<F.g.at(i).at(j)<<endl;
    }
    //cout<<endl;
    //cout<<ans.at(i)<<endl;
  }
  for(ll i=0;i<N;i++){
    cout<<ans.at(i)<<endl;
  }
  auto end= duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
  cerr<<end-start<<endl;
  return 0;
}
0