QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#699496#6766. Direction Settingjkliao#AC ✓2ms3920kbC++172.7kb2024-11-02 09:23:122024-11-02 09:23:13

Judging History

你现在查看的是最新测评结果

  • [2024-11-02 09:23:13]
  • 评测
  • 测评结果:AC
  • 用时:2ms
  • 内存:3920kb
  • [2024-11-02 09:23:12]
  • 提交

answer

#include <climits>
#include <iostream>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

struct Dinic {
  struct Edge {
    int to, rev;
    ll c, oc;
    ll flow() { return max(oc - c, 0LL); } // if you need flows
  };
  vi lvl, ptr, q;
  vector<vector<Edge>> adj;
  Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {}
  void addEdge(int a, int b, ll c, ll rcap = 0) {
    adj[a].push_back({b, sz(adj[b]), c, c});
    adj[b].push_back({a, sz(adj[a]) - 1, rcap, rcap});
  }
  ll dfs(int v, int t, ll f) {
    if (v == t || !f)
      return f;
    for (int &i = ptr[v]; i < sz(adj[v]); i++) {
      Edge &e = adj[v][i];
      if (lvl[e.to] == lvl[v] + 1)
        if (ll p = dfs(e.to, t, min(f, e.c))) {
          e.c -= p, adj[e.to][e.rev].c += p;
          return p;
        }
    }
    return 0;
  }
  ll calc(int s, int t) {
    ll flow = 0;
    q[0] = s;
    rep(L, 0, 31) do { // 'int L=30' maybe faster for random data
      lvl = ptr = vi(sz(q));
      int qi = 0, qe = lvl[s] = 1;
      while (qi < qe && !lvl[t]) {
        int v = q[qi++];
        for (Edge e : adj[v])
          if (!lvl[e.to] && e.c >> (30 - L))
            q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
      }
      while (ll p = dfs(s, t, LLONG_MAX))
        flow += p;
    }
    while (lvl[t])
      ;
    return flow;
  }
  bool leftOfMinCut(int a) { return lvl[a] != 0; }
};

void solve() {
  int n, m;
  cin >> n >> m;
  vector<ll> limits(n + 1);
  ll su = 0;
  for (int i = 1; i <= n; i++) {
    cin >> limits[i];
    su += limits[i];
  }
  Dinic d(n + m + 2);
  for (int i = 1; i <= n; i++) {
    d.addEdge(i, n + m + 1, limits[i]);
  }
  vector<pair<int, int>> edgelookup(m + 1);
  for (int i = 1; i <= m; i++) {
    d.addEdge(0, n + i, 1);
    int u, v;
    cin >> u >> v;
    edgelookup[i].first = d.adj[n + i].size();
    d.addEdge(n + i, u, 1);
    edgelookup[i].second = d.adj[n + i].size();
    d.addEdge(n + i, v, 1);
  }
  d.calc(0, n + m + 1);
  ll ans = 0;
  vector<int> sans(m);
  for (int i = 1; i <= m; i++) {
    Dinic::Edge utv = d.adj[n + i][edgelookup[i].second];
    Dinic::Edge vtu = d.adj[n + i][edgelookup[i].first];
    if (utv.flow() == 1) {
      sans[i - 1] = 0;
    } else if (vtu.flow() == 1) {
      sans[i - 1] = 1;
    } else {
      sans[i - 1] = 1;
      ans++;
    }
  }
  cout << ans << endl;
  for (int x : sans) {
    cout << x;
  }
  cout << endl;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int t;
  cin >> t;
  while (t--)
    solve();
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3876kb

input:

2
4 5
0 1 1 5
1 2
1 3
2 3
3 2
4 4
3 2
0 0 2
1 3
3 2

output:

2
00111
0
01

result:

ok 2 cases

Test #2:

score: 0
Accepted
time: 1ms
memory: 3920kb

input:

120
21 14
0 1 2 2 3 2 0 1 0 3 3 2 1 1 2 0 1 3 1 0 0
6 12
14 11
10 18
15 12
6 11
15 8
3 4
10 11
3 18
5 13
5 19
5 10
17 18
4 2
20 21
9 3 0 4 7 9 5 2 0 4 0 1 6 1 5 8 3 2 7 0
16 6
12 6
11 4
6 6
19 10
17 19
20 20
1 17
15 10
3 2
5 16
7 8
6 1
6 4
18 16
1 8
4 1
20 6
6 9
4 15
7 5
6 16
12 8 0 9 5 8
4 5
6 3
2 ...

output:

0
11111111111111
1
110111111011111110111
0
1111101111111011
2
111100111010111111111101
2
11111011111
2
000111111011111111010111
3
1101111111110111111011
0
11
0
1111111111111111111
0
0111011111111111
2
0111111001001
3
101101001
0
1111111
0
111111111111111
0
1111111111111111111
2
11110111110001111101
...

result:

ok 120 cases

Test #3:

score: 0
Accepted
time: 2ms
memory: 3692kb

input:

120
5 9
1 2 2 3 1
2 2
2 4
1 5
4 3
3 4
3 3
1 5
5 4
4 3
5 4
1 0 1 1 1
2 3
5 3
1 4
4 3
15 17
0 1 1 3 1 2 1 1 1 1 1 2 0 1 1
14 6
13 12
11 4
9 8
9 4
10 5
10 2
4 4
7 4
11 10
15 6
7 1
5 3
13 14
15 12
2 14
11 6
13 17
2 2 1 1 2 1 0 0 1 4 1 1 1
5 11
6 2
2 1
5 2
1 12
7 10
10 2
1 3
3 11
7 1
9 7
11 10
11 2
4 3
7...

output:

0
111111001
0
0111
0
00001011011100010
0
11110011101101001
0
1111111
5
1000111100111
4
1111011
2
101111111111011111111
0
11110
0
1111
0
11100
0
0101
5
11110100110101011
4
111111111
4
01111111
4
10101010111110110
0
110000001010100
0
000000111100001
0
0001001111010100000
4
10111000101
2
11101101101111...

result:

ok 120 cases

Test #4:

score: 0
Accepted
time: 1ms
memory: 3648kb

input:

120
12 11
7 0 0 1 0 0 1 1 0 0 1 0
12 1
1 9
1 2
1 8
3 1
6 1
10 1
1 7
5 1
11 1
4 1
11 10
1 0 0 0 1 0 7 0 0 0 1
7 11
6 7
7 5
8 7
10 7
9 7
7 4
7 1
7 2
3 7
24 23
1 1 1 1 0 5 1 1 1 1 0 1 1 1 1 0 1 0 1 1 1 1 0 1
20 6
12 6
6 9
1 6
6 15
14 6
6 2
6 18
23 6
10 6
3 6
6 16
6 7
17 6
6 13
19 6
6 22
5 6
4 6
6 11
6 ...

output:

0
01100000011
0
0000001010
0
11010101011101010011001
0
1010
0
0001101111101
0
1000110001101101011
0
10100101
0
00011011110
0
101001111011011001110110
0
1001111110010001
0
00100
0
0010101001
0
00101101011111101
0
100001110100100000
0
10011100001110011000
0
010010000101000111001
0
010
0
001100110
0
10...

result:

ok 120 cases

Test #5:

score: 0
Accepted
time: 2ms
memory: 3752kb

input:

10
300 300
0 0 0 1 6 3 0 36 0 0 0 0 8 53 23 2 8 7 8 4 1 0 3 5 0 4 0 0 2 5 5 5 7 0 0 4 3 0 0 21 2 7 6 2 0 0 0 4 0 67 3 4 9 3 1 1 0 6 2 2 0 0 0 0 0 41 5 0 0 15 2 5 0 6 5 4 0 7 4 4 6 8 0 7 2 6 0 4 4 0 8 22 1 2 6 3 0 3 6 0 0 3 0 0 6 3 0 63 3 7 7 4 0 0 0 26 0 0 1 3 6 1 1 0 0 22 0 2 0 0 1 7 4 1 0 1 0 4 0 ...

output:

31
011110111101011111110111111110110110111111011111111011011111111111101111101010111100110111111101100100101101111110101011011010111110001110110111111111101110111101111111111011100001101110011110111110110110001111111111101111110111101101111100111101011111101010110001011110100111110011111101001111111...

result:

ok 10 cases

Test #6:

score: 0
Accepted
time: 1ms
memory: 3652kb

input:

10
30 300
17 12 25 27 17 21 21 24 10 18 23 19 25 26 14 23 9 27 22 19 25 15 13 22 25 12 20 10 27 22
29 11
25 9
23 10
9 25
25 12
20 1
18 9
2 1
30 14
2 10
6 18
3 27
10 17
14 4
21 2
2 15
28 4
7 8
19 13
14 13
8 7
10 18
4 18
4 19
5 16
23 14
21 24
24 12
15 24
23 29
11 25
10 4
23 9
27 13
18 26
12 24
23 3
27...

output:

0
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111...

result:

ok 10 cases

Test #7:

score: 0
Accepted
time: 2ms
memory: 3776kb

input:

10
290 299
0 3 2 1 2 0 0 0 0 1 0 3 2 1 3 0 0 3 1 0 3 1 0 1 0 0 0 1 0 3 0 1 0 2 1 7 0 1 0 3 0 2 1 2 1 2 0 1 0 0 2 2 1 0 0 0 0 1 2 0 2 1 0 1 2 1 1 0 1 0 0 0 1 1 1 1 1 2 2 1 3 0 0 1 2 0 1 0 1 2 1 1 0 0 2 1 2 1 1 0 1 0 2 0 0 1 1 0 0 1 0 4 1 2 2 1 1 1 1 2 2 3 1 1 1 0 1 2 1 1 2 0 1 1 1 1 0 0 2 1 1 0 0 1 1...

output:

0
1111100000010110001101010001000010011010011010010100011001001000000010011100100011101100001001101110000111111110010001110000011000000111011001011110101010101110110100001110001000111100000010111010011001100101011100000110001100011001001101000001111100011000001010001001100100011101001000000111010101...

result:

ok 10 cases

Test #8:

score: 0
Accepted
time: 2ms
memory: 3716kb

input:

10
300 299
1 1 1 1 1 1 0 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 1 142 1 0 0 1 1 1 0 0 0 1 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 1 0 0 1 0 1 1 0 1 1 1 1 0...

output:

0
0111101010001110110101100110100101010010001000100000011000100101111101111101010111000110110010110011010000011011011101000000111011111001010111001011010110010000110010110000010111011100111100100011110110010010101111110011110011001001101011010100101001110111101100000101101111110010001110011010111011...

result:

ok 10 cases