結果

問題 No.1640 簡単な色塗り
ユーザー harurunharurun
提出日時 2021-06-19 16:09:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 5,101 bytes
コンパイル時間 1,359 ms
コンパイル使用メモリ 107,780 KB
実行使用メモリ 65,444 KB
最終ジャッジ日時 2023-09-12 01:38:03
合計ジャッジ時間 25,152 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,708 KB
testcase_01 AC 2 ms
4,528 KB
testcase_02 AC 3 ms
4,456 KB
testcase_03 AC 3 ms
4,640 KB
testcase_04 AC 253 ms
50,696 KB
testcase_05 AC 275 ms
62,148 KB
testcase_06 AC 3 ms
4,528 KB
testcase_07 AC 3 ms
4,572 KB
testcase_08 AC 3 ms
4,704 KB
testcase_09 AC 3 ms
4,504 KB
testcase_10 AC 346 ms
37,316 KB
testcase_11 AC 272 ms
31,868 KB
testcase_12 AC 231 ms
29,320 KB
testcase_13 AC 641 ms
56,204 KB
testcase_14 AC 625 ms
56,608 KB
testcase_15 AC 151 ms
21,880 KB
testcase_16 AC 190 ms
25,928 KB
testcase_17 AC 493 ms
48,956 KB
testcase_18 AC 27 ms
8,536 KB
testcase_19 AC 234 ms
29,636 KB
testcase_20 AC 344 ms
36,344 KB
testcase_21 AC 263 ms
31,320 KB
testcase_22 AC 19 ms
7,408 KB
testcase_23 AC 360 ms
38,836 KB
testcase_24 AC 72 ms
14,612 KB
testcase_25 AC 220 ms
29,056 KB
testcase_26 AC 399 ms
44,828 KB
testcase_27 AC 138 ms
20,984 KB
testcase_28 AC 539 ms
52,512 KB
testcase_29 AC 402 ms
44,000 KB
testcase_30 AC 24 ms
12,720 KB
testcase_31 AC 193 ms
56,588 KB
testcase_32 AC 171 ms
50,156 KB
testcase_33 AC 108 ms
34,980 KB
testcase_34 AC 140 ms
46,052 KB
testcase_35 AC 108 ms
34,908 KB
testcase_36 AC 25 ms
12,388 KB
testcase_37 AC 34 ms
15,556 KB
testcase_38 AC 179 ms
52,368 KB
testcase_39 AC 69 ms
25,344 KB
testcase_40 AC 72 ms
25,060 KB
testcase_41 AC 153 ms
45,900 KB
testcase_42 AC 86 ms
27,908 KB
testcase_43 AC 90 ms
28,696 KB
testcase_44 AC 88 ms
28,584 KB
testcase_45 AC 66 ms
24,236 KB
testcase_46 AC 30 ms
13,752 KB
testcase_47 AC 18 ms
10,268 KB
testcase_48 AC 193 ms
53,448 KB
testcase_49 AC 9 ms
7,044 KB
testcase_50 AC 3 ms
4,396 KB
testcase_51 AC 3 ms
4,588 KB
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt AC 1,914 ms
120,224 KB
07_evil_02.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/dsu>
#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;


        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;
            ll level_v = level[v];
            for (long long& i = iter[v]; i < (long long)g[v].size(); i++) {
                _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;
                res += d;
                if (res == up) break;
            }
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit) {
            bfs();
            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(){
  auto start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
  ll N,A,B;
  cin>>N;
  mf_graph<ll> F(2*N+2);
  dsu uf(N);
  vector<ll> had_edge(N);
  for(ll i=0;i<N;i++){
    cin>>A>>B;
    A--;B--;
    uf.merge(A,B);
    had_edge.at(A)++;
    had_edge.at(B)++;
    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);
  }
  auto g=uf.groups();
  for(auto i:g){
    ll cnt=0;
    for(ll j:i){
      cnt+=had_edge.at(j);
    }
    cnt/=2;
    if(cnt!=(ll)i.size()){
      cout<<"No\n";
      return 0;
    }
  }
  ll cnt=F.flow(2*N,2*N+1);
  if(cnt!=N){
    cout<<"No\n";
    return 0;
  }
  //ここから構築
  cout<<"Yes\n";
  for(ll i=N;i<2*N;i++){
    ll s=F.g.at(i).size();
    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;
      }
    }
  }
  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<<"time:"<<end-start<<endl;
  return 0;
}
0