답안 #293850

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
293850 2020-09-08T13:03:59 Z 반딧불(#5811) Acrobat (balkan16_acrobat) C++17
100 / 100
685 ms 97216 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

void massert(bool cond){
    if(!cond){
        printf("-1");
        exit(0);
    }
}

struct Answer{
    int a, b, c;
    Answer(){}
    Answer(int a, int b, int c): a(a), b(b), c(c){}
};
vector<Answer> ans;

int n, m;
int edgeX[300002], edgeY[300002];
vector<int> linkDual[300002];
int degA[300002], degB[300002];

bool visited[300002];
int group[300002], groupNum, groupRoot[300002];
int DP[300002];
vector<int> spanningChild[300002];

unordered_set<int> edgeSet[300002];

void dfs(int x){
    visited[x] = 1, group[x] = groupNum;
    for(auto y: linkDual[x]){
        if(visited[y]) continue;
        dfs(y);
        spanningChild[x].push_back(y);
    }
}
void dfs2(int x){
    for(auto y: spanningChild[x]){
        dfs2(y);
        DP[x] += DP[y];

        if(DP[y]%2==1){
            if(edgeSet[x].find(y) != edgeSet[x].end()) ans.push_back({1, x, y}), degA[x]--, degB[x]++, degA[y]++, degB[y]--;
            else ans.push_back({1, y, x}), degA[y]--, degB[y]++, degA[x]++, degB[x]--;
        }
    }
}

int realDegA[300002], realDegB[300002];

int main(){
    scanf("%d %d", &n, &m);
    for(int i=1; i<=m; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        edgeX[i] = x, edgeY[i] = y;
        linkDual[x].push_back(y), linkDual[y].push_back(x);
        edgeSet[x].insert(y);
        degA[x]++, degB[y]++;
        realDegA[x]++, realDegB[y]++;
    }

    for(int i=1; i<=n; i++){
        if(!visited[i]){
            groupRoot[++groupNum] = i;
            dfs(i);
        }
    }

    vector<pair<int, int> > degAVec;
    for(int i=1; i<=n; i++){
        if(degA[i]%2 == 1) degAVec.push_back({group[i], i});
    }
    sort(degAVec.begin(), degAVec.end());

    massert(degAVec.size() % 2 == 0);
    for(int i=0; i<(int)degAVec.size(); i+=2){
        massert(group[degAVec[i].first] == group[degAVec[i+1].first]);
        DP[degAVec[i].second]++, DP[degAVec[i+1].second]++;
    }

    for(int i=1; i<=groupNum; i++){
        dfs2(groupRoot[i]);
    }

    vector<int> oddB;
    for(int i=1; i<=n; i++) if(degB[i]%2) oddB.push_back(i);
    massert(oddB.size() % 2 == 0);
    sort(oddB.begin(), oddB.end());

    if(oddB.size() == 2 && oddB.back() - oddB.front() == 1){
        for(int i=1; i<n; i++) if(i != oddB.front()) ans.push_back({2, i, i+1});
        ans.push_back({2, n, 1});
    }
    else{
        int ts = (int)oddB.size() / 2;
        for(int i=0; i<ts; i++) ans.push_back({2, oddB[i], oddB[i+ts]});
        for(int i=1; i<n; i++) ans.push_back({2, i, i+1});
        ans.push_back({2, n, 1});
    }

    for(int i=0; i<(int)ans.size(); i++){
        if(ans[i].a == 1) realDegA[ans[i].b]--, realDegB[ans[i].b]++, realDegA[ans[i].c]++, realDegB[ans[i].c]--;
        else realDegB[ans[i].b]++, realDegB[ans[i].c]++;
    }

    for(int i=1; i<=n; i++) massert(realDegA[i]%2==0 && realDegB[i]%2==0);

    printf("%d\n", (int)ans.size());
    for(int i=0; i<(int)ans.size(); i++){
        printf("%d %d %d\n", ans[i].a, ans[i].b, ans[i].c);
    }
}

Compilation message

main.cpp: In function 'int main()':
main.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   56 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   59 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 30848 KB Output is correct
2 Correct 28 ms 30840 KB Output is correct
3 Correct 23 ms 30976 KB Output is correct
4 Correct 26 ms 30848 KB Output is correct
5 Correct 23 ms 30960 KB Output is correct
6 Correct 23 ms 30976 KB Output is correct
7 Correct 23 ms 30976 KB Output is correct
8 Correct 26 ms 30956 KB Output is correct
9 Correct 30 ms 30976 KB Output is correct
10 Correct 26 ms 30976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 30840 KB Output is correct
2 Correct 23 ms 30848 KB Output is correct
3 Correct 23 ms 30848 KB Output is correct
4 Correct 23 ms 30976 KB Output is correct
5 Correct 24 ms 30976 KB Output is correct
6 Correct 23 ms 30976 KB Output is correct
7 Correct 33 ms 30976 KB Output is correct
8 Correct 22 ms 30976 KB Output is correct
9 Correct 26 ms 30976 KB Output is correct
10 Correct 22 ms 30968 KB Output is correct
11 Correct 25 ms 31096 KB Output is correct
12 Correct 24 ms 31104 KB Output is correct
13 Correct 24 ms 31104 KB Output is correct
14 Correct 29 ms 30976 KB Output is correct
15 Correct 23 ms 31104 KB Output is correct
16 Correct 23 ms 30976 KB Output is correct
17 Correct 23 ms 30976 KB Output is correct
18 Correct 25 ms 31104 KB Output is correct
19 Correct 25 ms 31104 KB Output is correct
20 Correct 24 ms 30976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 30848 KB Output is correct
2 Correct 24 ms 30848 KB Output is correct
3 Correct 24 ms 30848 KB Output is correct
4 Correct 24 ms 30880 KB Output is correct
5 Correct 24 ms 30976 KB Output is correct
6 Correct 23 ms 30972 KB Output is correct
7 Correct 23 ms 30984 KB Output is correct
8 Correct 29 ms 30976 KB Output is correct
9 Correct 24 ms 30976 KB Output is correct
10 Correct 24 ms 30976 KB Output is correct
11 Correct 24 ms 31096 KB Output is correct
12 Correct 24 ms 31104 KB Output is correct
13 Correct 23 ms 31104 KB Output is correct
14 Correct 26 ms 30976 KB Output is correct
15 Correct 24 ms 31104 KB Output is correct
16 Correct 25 ms 30976 KB Output is correct
17 Correct 23 ms 30976 KB Output is correct
18 Correct 24 ms 31104 KB Output is correct
19 Correct 24 ms 31104 KB Output is correct
20 Correct 25 ms 30916 KB Output is correct
21 Correct 630 ms 76308 KB Output is correct
22 Correct 685 ms 88976 KB Output is correct
23 Correct 194 ms 50264 KB Output is correct
24 Correct 141 ms 43640 KB Output is correct
25 Correct 169 ms 46072 KB Output is correct
26 Correct 512 ms 68444 KB Output is correct
27 Correct 396 ms 51748 KB Output is correct
28 Correct 310 ms 50468 KB Output is correct
29 Correct 431 ms 97216 KB Output is correct
30 Correct 404 ms 92996 KB Output is correct
31 Correct 259 ms 49144 KB Output is correct
32 Correct 468 ms 95112 KB Output is correct
33 Correct 24 ms 31104 KB Output is correct
34 Correct 23 ms 31104 KB Output is correct
35 Correct 627 ms 76272 KB Output is correct
36 Correct 206 ms 48888 KB Output is correct
37 Correct 24 ms 31104 KB Output is correct
38 Correct 23 ms 31104 KB Output is correct