QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#629094#4504. Book ClubjdelaureRE 0ms77892kbC113.0kb2024-10-11 03:42:012024-10-11 03:42:03

Judging History

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

  • [2024-10-11 03:42:03]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:77892kb
  • [2024-10-11 03:42:01]
  • 提交

answer

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

uint16_t graphAsArray[20002][20002];
//uint16_t graphAsGraph[20002][20002]; 
uint16_t graphNbEdges[20002];
uint16_t path[20002];

int nbPeople;

/*
void sanityCheck() {
    for (int src = 0; src <= nbPeople*2+1; src++) {
        for (int i = 0; i < graphNbEdges[src]; i++) {
            int dst = graphAsArray[src][i];
            if (!graphAsGraph[src][dst]) {
                fprintf(stderr, "what the fuck %d %d\n", src, dst);
                printf("[%i][%i]\n", src, i);
            }
        }
    }
}
*/

uint16_t getEdgeIndex(uint16_t src, uint16_t dst) {
    for (uint16_t i = 0; i < graphNbEdges[src]; i++) {
        if (graphAsArray[src][i] == dst) {
            return i;
        }
    }
    return (uint16_t)-1;
}

void addEdge(uint16_t src, uint16_t dst) {
    //if (!graphAsGraph[src][dst]) {
        int index = graphNbEdges[src]++;
        //graphAsGraph[src][dst] = index + 1;
        graphAsArray[src][index] = dst;

    //}

    //sanityCheck();
}

void removeEdge(uint16_t src, uint16_t dst) {
    int index, replace;
    uint16_t newDst;

    //if (graphAsGraph[src][dst]) {
        replace = --graphNbEdges[src];
        index = getEdgeIndex(src, dst); //graphAsGraph[src][dst] - 1;
        newDst = graphAsArray[src][replace];

        //graphAsGraph[src][dst] = 0;
        graphAsArray[src][index] = newDst;
        //graphAsGraph[src][newDst] = index + 1;
    //}

    //sanityCheck();
}

bool searchPath(uint16_t source, uint16_t target) {
    if (source == target)
        return true;
    
    for (int i = 0; i < graphNbEdges[source]; i++) {
        uint16_t dst = graphAsArray[source][i];
        if (path[dst] == (uint16_t)-1) {
            path[dst] = source;
            if (searchPath(dst, target))
                return true;
        }
    }

    return false;
}

int fordFulkerson(uint16_t source, uint16_t target) {
    uint16_t u, v;
    int maxFlow = 0;

    for (;;) {
        memset(path, -1, sizeof(path));
        if (!searchPath(source, target))
            break;

        //printf("%i %i\n", source, target);
        
        v = target;
        while (v != source) {
            u = path[v];
            removeEdge(u, v);
            addEdge(v, u);
            v = path[v];
        }

        maxFlow++;
    }

    return maxFlow;
}

int main(void) {
    //freopen("J2.in", "r", stdin);

    int nbDeclarations;
    int person, preference;
    scanf("%d %d", &nbPeople, &nbDeclarations);

    for (int i = 0; i < nbDeclarations; i++) {
        scanf("%d %d", &person, &preference);
        addEdge(person*2, preference*2+1);
    }

    for (int i = 0; i < nbPeople; i++) {
        addEdge(nbPeople*2, i*2);
        addEdge(i*2+1, nbPeople*2+1);
    }

    int result = fordFulkerson(nbPeople*2, nbPeople*2+1);
    if (result == nbPeople) {
        printf("YES\n");
    } else {
        printf("NO\n");
    }
}

詳細信息

Test #1:

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

input:

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

output:

YES

result:

ok single line: 'YES'

Test #2:

score: 0
Accepted
time: 0ms
memory: 77892kb

input:

1000 5000
0 271
0 320
0 325
0 378
0 562
0 942
0 957
1 88
1 235
1 271
1 579
1 604
1 751
1 875
1 929
2 70
2 403
2 470
2 909
2 912
3 352
3 657
3 810
4 201
4 361
4 414
4 418
4 505
4 531
4 597
4 833
5 14
5 123
5 281
5 878
5 938
6 311
6 392
6 564
6 994
7 246
7 353
7 372
7 710
7 751
7 772
8 149
8 435
8 682...

output:

NO

result:

ok single line: 'NO'

Test #3:

score: -100
Runtime Error

input:

10000 10000
0 3378
1 8751
2 9999
3 7769
4 6001
5 9998
6 9311
7 9996
8 7722
9 1572
10 9994
11 3865
12 186
13 7614
14 9992
15 9991
16 4424
17 9807
18 5031
19 7909
20 4994
21 292
22 9990
23 8180
24 5837
25 9989
26 9554
27 7552
28 8027
29 5135
30 9988
31 4067
32 702
33 9984
34 8831
35 6971
36 7354
37 81...

output:


result: