QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#485170#8593. Coinfractal6 6ms33196kbC++172.7kb2024-07-20 14:39:072024-07-20 14:39:07

Judging History

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

  • [2024-07-20 14:39:07]
  • 评测
  • 测评结果:6
  • 用时:6ms
  • 内存:33196kb
  • [2024-07-20 14:39:07]
  • 提交

answer

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

#define F first
#define S second 
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define make_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 Rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int N = 1e6 + 200;
const int M = 1e6;
const int inf = 2e9 + 3;
const ll INF = 1e18;

int n, m;
vector<int> g[N];

int can[1001][1001];
int ans[N], now[N], d[N];

int sz = 0;
int u[N], C, pos[N];

void dfs(int v) {
    u[v] = C;
    for (auto to : g[v]) {
        if (u[to] == C) continue;
        dfs(to);
    }
    ++sz;
    if (pos[v] && pos[v] != sz) {
        ans[v] = -1;
    }
    pos[v] = sz;
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) 
        ans[i] = -1;
    if (n <= 1000) {
        vector<int> Ans(n + 1, -1);
        for (int i = 1, x, y; i <= m; ++i) {
            cin >> x >> y;
            g[x].push_back(y);
            for (int j = 1; j <= n; ++j) {
                ans[j] = i;
                pos[j] = 0;
            }
            int T = 50;
            while (T--) {
                vector<int> v(n);
                iota(v.begin(), v.end(), 1);
                shuffle(v.begin(), v.end(), rng);
                for (int j = 1; j <= n; ++j) 
                    sort(g[j].begin(), g[j].end());
                ++C;
                sz = 0;
                for (int j : v) {
                    if (u[j] != C) {
                        dfs(j);
                    }
                }
            }
            for (int j = 1; j <= n; ++j) {
                if (ans[j] == i && Ans[j] == -1) Ans[j] = i;
            }
        }
        for (int i = 1; i <= n; ++i)
            cout << Ans[i] << " \n"[i == n];
        return 0;
    }
    for (int i = 1, x, y; i <= m; ++i) {
        cin >> x >> y;
        g[x].push_back(y);
        d[y]++;
    }
    C = 1;
    for (int i = 1; i <= n; ++i) {
        ans[i] = m;
        if (u[i] == 0) {
            dfs(i);
        }
    }
    int T = 5000;
    while (T--) {
        vector<int> v(n);
        iota(v.begin(), v.end(), 1);
        shuffle(v.begin(), v.end(), rng);
        for (int i = 1; i <= n; ++i) 
            shuffle(g[i].begin(), g[i].end(), rng);
        ++C;
        sz = 0;
        for (int i : v) {
            if (u[i] != C) {
                dfs(i);
            }
        }
    }
    for (int i = 1; i <= n; ++i)
        cout << ans[i] << " \n"[i == n];
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 6
Accepted

Test #1:

score: 6
Accepted
time: 0ms
memory: 31624kb

input:

4 4
2 4
3 1
4 1
2 3

output:

3 4 -1 -1

result:

ok ac

Test #2:

score: 6
Accepted
time: 0ms
memory: 33080kb

input:

6 8
1 5
5 4
6 2
2 5
4 3
6 1
6 5
2 1

output:

8 8 5 5 5 6

result:

ok ac

Test #3:

score: 6
Accepted
time: 6ms
memory: 32048kb

input:

2 1
1 2

output:

1 1

result:

ok ac

Test #4:

score: 6
Accepted
time: 3ms
memory: 33084kb

input:

6 12
1 5
5 4
6 2
2 5
4 3
6 5
1 5
1 5
2 4
6 3
1 3
4 3

output:

-1 -1 5 5 5 -1

result:

ok ac

Test #5:

score: 6
Accepted
time: 5ms
memory: 32796kb

input:

7 20
1 6
6 3
1 4
1 5
1 7
1 2
1 5
2 3
4 5
7 2
2 4
5 3
6 3
1 3
4 3
7 5
2 6
4 6
7 2
7 5

output:

6 17 12 18 -1 -1 17

result:

ok ac

Test #6:

score: 6
Accepted
time: 3ms
memory: 31700kb

input:

7 20
5 6
1 3
3 6
4 1
7 4
2 5
4 3
2 6
7 5
4 6
2 6
2 1
4 5
1 3
1 5
7 1
7 6
4 1
7 6
3 6

output:

15 -1 -1 -1 -1 6 -1

result:

ok ac

Test #7:

score: 6
Accepted
time: 3ms
memory: 31624kb

input:

7 20
7 6
4 5
6 4
3 6
4 1
6 2
3 5
5 2
7 6
1 2
3 6
6 4
7 1
6 1
7 1
4 5
3 6
3 5
4 5
3 1

output:

-1 10 -1 8 -1 6 -1

result:

ok ac

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #8:

score: 0
Wrong Answer
time: 3ms
memory: 33196kb

input:

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

output:

-1 55 55 31 31 31 31 -1 29 55 29 31 31 -1 71 -1 -1 84 55 31

result:

wrong answer wa

Subtask #3:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%