CSES - Datatähti Open 2017 - Results
Submission details
Task:Tunnels
Sender:egor.lifar
Submission time:2017-01-22 00:35:23 +0200
Language:C++
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED16
#2ACCEPTED31
#3ACCEPTED53
Test results
testverdicttimegroup
#1ACCEPTED0.07 s1details
#2ACCEPTED0.07 s1details
#3ACCEPTED0.07 s1details
#4ACCEPTED0.06 s1details
#5ACCEPTED0.05 s1details
#6ACCEPTED0.06 s2details
#7ACCEPTED0.07 s2details
#8ACCEPTED0.07 s2details
#9ACCEPTED0.07 s2details
#10ACCEPTED0.06 s2details
#11ACCEPTED0.42 s3details
#12ACCEPTED0.21 s3details
#13ACCEPTED0.11 s3details
#14ACCEPTED0.10 s3details
#15ACCEPTED0.08 s3details

Code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <cassert>
 
  
  
using namespace std;
  
  
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
/** Interface */
  
inline int readChar();
template <class T = int> inline T readInt(); 
template <class T> inline void writeInt( T x, char end = 0 );
inline void writeChar( int x ); 
inline void writeWord( const char *s );
  
/** Read */
  
static const int buf_size = 4096;
  
inline int getChar() {
    static char buf[buf_size];
    static int len = 0, pos = 0;
    if (pos == len)
        pos = 0, len = fread(buf, 1, buf_size, stdin);
    if (pos == len)
        return -1;
    return buf[pos++];
}
  
inline int readChar() {
    int c = getChar();
    while (c <= 32)
        c = getChar();
    return c;
}
  
template <class T>
inline T readInt() {
    int s = 1, c = readChar();
    T x = 0;
    if (c == '-')
        s = -1, c = getChar();
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getChar();
    return s == 1 ? x : -x;
}
  
/** Write */
  
static int write_pos = 0;
static char write_buf[buf_size];
  
inline void writeChar( int x ) {
    if (write_pos == buf_size)
        fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
    write_buf[write_pos++] = x;
}
  
template <class T> 
inline void writeInt( T x, char end ) {
    if (x < 0)
        writeChar('-'), x = -x;
  
    char s[24];
    int n = 0;
    while (x || !n)
        s[n++] = '0' + x % 10, x /= 10;
    while (n--)
        writeChar(s[n]);
    if (end)
        writeChar(end);
}
  
inline void writeWord( const char *s ) {
    while (*s)
        writeChar(*s++);
}
  
struct Flusher {
    ~Flusher() {
        if (write_pos)
            fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
    }
} flusher;
  

#define left left228
#define right right228  
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
const int MAXN = 200003;


#define INF 1000000002


struct edge {
    int a, b, cap, flow;
};


int n, s, t, d[3 * MAXN], ptr[3 * MAXN], q[3 * MAXN];
vector<edge> e;
vector<int> g[3 * MAXN];

 
void add_edge(int a, int b, int cap) {
    edge e1 = {a, b, cap, 0};
    edge e2 = {b, a, 0, 0};
    g[a].push_back((int)e.size());
    e.push_back(e1);
    g[b].push_back((int)e.size());
    e.push_back(e2);
}


bool bfs() {
    int qh = 0, qt = 0;
    q[qt++] = s;
    memset(d, -1, sizeof(d));
    d[s] = 0;
    while (qh < qt && d[t] == -1) {
        int v = q[qh++];
        for (unsigned int i = 0; i < g[v].size(); i++) {
            int id = g[v][i], to = e[id].b;
            if (d[v] != -1 && d[to] == -1 && e[id].flow < e[id].cap) {
                q[qt++] = to;
                d[to] = d[v] + 1;
            }
        }
    }
    return d[t] != -1;
}
 
int dfs(int v, int flow) {
    if (!flow) { 
        return 0;
    }
    if (v == t) { 
        return flow;
    }
    for (; ptr[v] < (int)g[v].size(); ptr[v]++) {
        int id = g[v][ptr[v]], to = e[id].b;
        if (d[to] != d[v] + 1)  {
            continue;
        }
        int pushed = dfs(to, min(flow, e[id].cap - e[id].flow));
        if (pushed) {
            e[id].flow += pushed;
            e[id ^ 1].flow -= pushed;
            return pushed;
        }
    }
    return 0;
}
 

int dinic() {
    int flow = 0;
    for (;;) {
        if (!bfs()) {
            break;
        }
        memset(ptr, 0, sizeof(ptr));
        while (int pushed = dfs(s, INF)) {
            flow += pushed;
        }
    }
    return flow;
}

int m;
vector<pair<int, int> > gg[MAXN];
vector<pair<int, int> > g1[MAXN];


int main() {
    n = readInt(), m = readInt();
    for (int i = 0; i < m; i++) {
        int a = readInt(), b = readInt();
        a--, b--;
        gg[a].push_back(make_pair(b, i));
        g1[b].push_back(make_pair(a, i));
    }
    for (int i = 0; i < n; i++) {
        for (auto x: g1[i]) {
            for (auto y: gg[i]) {
                add_edge(x.second + 1, y.second + 1 + m, 1);
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        add_edge(0, i, 1);
    }
    for (int i = m + 1; i <= 2 * m; i++) {
        add_edge(i, 2 * m + 1, 1);
    }
    s = 0;
    t = 2 * m + 1;
    n = 2 * m + 2;
    memset(ptr, 0, sizeof(ptr));
    memset(q, 0, sizeof(q));
    memset(d, 0, sizeof(d));
    dinic();
    int size = 0;
    for (int i = 0; i < (int)e.size(); i++) {
        if (e[i].a == 0) {
            size += e[i].flow;
        }
    }
    writeInt(m - size, '\n');
    return 0;
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10 20
4 5
6 4
5 1
5 9
...

correct output
11

user output
11

Test 2

Group: 1

Verdict: ACCEPTED

input
10 10
7 3
5 2
9 7
1 5
...

correct output
5

user output
5

Test 3

Group: 1

Verdict: ACCEPTED

input
10 5
5 7
3 8
5 8
3 7
...

correct output
4

user output
4

Test 4

Group: 1

Verdict: ACCEPTED

input
10 4
9 1
6 8
7 1
5 7

correct output
3

user output
3

Test 5

Group: 1

Verdict: ACCEPTED

input
10 2
10 6
2 1

correct output
2

user output
2

Test 6

Group: 2

Verdict: ACCEPTED

input
100 200
24 40
25 6
36 93
92 90
...

correct output
97

user output
97

Test 7

Group: 2

Verdict: ACCEPTED

input
100 100
98 37
91 37
60 92
46 27
...

correct output
60

user output
60

Test 8

Group: 2

Verdict: ACCEPTED

input
100 50
74 95
53 72
69 85
14 13
...

correct output
34

user output
34

Test 9

Group: 2

Verdict: ACCEPTED

input
100 40
28 76
10 81
13 52
46 83
...

correct output
29

user output
29

Test 10

Group: 2

Verdict: ACCEPTED

input
100 20
27 35
72 92
56 4
64 80
...

correct output
18

user output
18

Test 11

Group: 3

Verdict: ACCEPTED

input
100000 200000
89244 59358
22943 56710
63331 89437
56581 38400
...

correct output
102510

user output
102510

Test 12

Group: 3

Verdict: ACCEPTED

input
100000 100000
21701 85599
61542 21474
38081 29362
46316 64038
...

correct output
60593

user output
60593

Test 13

Group: 3

Verdict: ACCEPTED

input
100000 50000
86469 4833
16351 35505
59315 33011
95464 16985
...

correct output
35933

user output
35933

Test 14

Group: 3

Verdict: ACCEPTED

input
100000 40000
5392 23534
63204 45619
74330 25925
59678 88427
...

correct output
30074

user output
30074

Test 15

Group: 3

Verdict: ACCEPTED

input
100000 20000
80156 16531
71753 77661
7028 33389
17168 646
...

correct output
16882

user output
16882