답안 #729340

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
729340 2023-04-23T20:19:28 Z jerzyk Joker (BOI20_joker) C++14
100 / 100
249 ms 13428 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 200 * 1000 + 7;

struct dynamic_mst
{
    int fau[N], mn[N], pp = -1, sum[N];
    bool ifb = true;
    vector<pair<int, pair <int,int>>> edges;

    void start(int v)
    {
    	sum[v] = 1;
    	fau[v] = v;
    	mn[v] = 1;
    }
    pair<int, int> find(int v)
    {
    	pair<int, int> w;
    	if(fau[v] == v)
    		return make_pair(v, mn[v]);

    	w = find(fau[v]);
    	w.second *= mn[v];
    	return w;
    }

    void add(pair<int,int> edge)
    {
       	pair<int, int> w1, w2;
       	w1 = find(edge.first);
       	w2 = find(edge.second);
       	if(w1.first == w2.first)
       	{
       		if(w1.second == w2.second)
       		{
       			ifb = false;
       			if(pp == -1)
       				pp = edges.size() + 1;
       		}
       		edges.push_back(make_pair(0, edge));
       	}else
       	{
       		if(sum[w1.first] < sum[w2.first])
       		{
       			swap(w1, w2);
       			swap(edge.first, edge.second);
       		}
       		sum[w1.first] += sum[w2.first];
            if(mn[w1.first] == -1)
            {
                mn[w1.first] *= -1;
                mn[w2.first] *= -1; 
            }
       		if(w1.second == w2.second)
            {
       			mn[w2.first] *= -1;
            }

       		fau[w2.first] = w1.first;
       		edges.push_back(make_pair(1, make_pair(w1.first, w2.first)));
       	}
    }
    void remove()
    {
        if(edges.size() == 0)
            return;

        if(edges.back().first == 1)
        {
        	fau[edges.back().second.second] = edges.back().second.second;
       		sum[edges.back().second.first] -= sum[edges.back().second.second];
        }
        if((int)edges.size() == pp)
        {
        	pp = -1;
        	ifb = true;
        }
        edges.pop_back();
    }
    void print_colors()
    {
        for (int i = 1; i <= 5; i++)
            cout << i << ", " << mn[i] << " | ";
        cout << "\n";

    }
    int check()
    {
        for (int i = 0; i < (int)edges.size(); ++i)
        {
            cout << edges[i].second.first << " " << edges[i].second.second << " : ";
        }
        cout << "\n";
        return edges.size();
    }
    bool is_bipartite()
    {
        return ifb;
    }
};

dynamic_mst mst;
int w[N];
pair<int, int> kr[N];

void DAC(int p, int k, int aw)
{
    int i, v;
    v = (p + k) / 2;
    for(i = k; i > v; --i)
        mst.add(kr[i]);

    if(v != p)
    {
        mst.add(kr[v]);
        DAC(p, v - 1, aw);
        mst.remove();
    }

    i = aw;
    while(mst.is_bipartite() && i < v)
    {
        ++i;
        mst.add(kr[i]);
    }
    if(i > aw && mst.is_bipartite() == false)
    {
        --i;
        mst.remove();
    }
    w[v] = i;
    if(!mst.is_bipartite())
        w[v] = -1;

    for( ; i > aw; --i)
        mst.remove();

    for(i = v + 1; i <= k; ++i)
        mst.remove();
    
    for(i = aw + 1; i <= w[v]; ++i)
        mst.add(kr[i]);

    i = max(aw, w[v]);
    if(v != k)
        DAC(v + 1, k, i);
    
    for(i = w[v]; i > aw; --i)
        mst.remove();
}

void Odpowiadaj(int m)
{
    int i, a, b;
    for(i = 1; i <= m; ++i)
    {
        cin >> a >> b;
        if(w[b] >= a - 1)
            cout << "NO\n";
        else
            cout << "YES\n";
    }
}

void Wczytaj(int &n, int &m)
{
    int k, i, a, b;
    cin >> k >> n >> m;
    for(i = 1; i <= k; ++i)
        mst.start(i);
    for(i = 1; i <= n; ++i)
    {
        cin >> a >> b;
        kr[i] = make_pair(a, b);
    }
}

void Joker()
{
    int n, m;
    Wczytaj(n, m);
    DAC(1, n, 0);
    Odpowiadaj(m);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	Joker();

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 472 KB Output is correct
31 Correct 2 ms 412 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 2 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 1 ms 476 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 480 KB Output is correct
39 Correct 2 ms 468 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 484 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 472 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 476 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 468 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 2 ms 468 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 154 ms 10944 KB Output is correct
4 Correct 212 ms 11632 KB Output is correct
5 Correct 153 ms 12132 KB Output is correct
6 Correct 149 ms 11004 KB Output is correct
7 Correct 168 ms 10968 KB Output is correct
8 Correct 150 ms 9852 KB Output is correct
9 Correct 171 ms 10404 KB Output is correct
10 Correct 235 ms 11860 KB Output is correct
11 Correct 174 ms 10868 KB Output is correct
12 Correct 187 ms 11952 KB Output is correct
13 Correct 129 ms 9836 KB Output is correct
14 Correct 172 ms 10432 KB Output is correct
15 Correct 208 ms 11360 KB Output is correct
16 Correct 239 ms 12156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 154 ms 10944 KB Output is correct
30 Correct 212 ms 11632 KB Output is correct
31 Correct 153 ms 12132 KB Output is correct
32 Correct 149 ms 11004 KB Output is correct
33 Correct 168 ms 10968 KB Output is correct
34 Correct 150 ms 9852 KB Output is correct
35 Correct 171 ms 10404 KB Output is correct
36 Correct 235 ms 11860 KB Output is correct
37 Correct 174 ms 10868 KB Output is correct
38 Correct 187 ms 11952 KB Output is correct
39 Correct 129 ms 9836 KB Output is correct
40 Correct 172 ms 10432 KB Output is correct
41 Correct 208 ms 11360 KB Output is correct
42 Correct 239 ms 12156 KB Output is correct
43 Correct 150 ms 11216 KB Output is correct
44 Correct 222 ms 12064 KB Output is correct
45 Correct 164 ms 12468 KB Output is correct
46 Correct 146 ms 11308 KB Output is correct
47 Correct 172 ms 11192 KB Output is correct
48 Correct 202 ms 11116 KB Output is correct
49 Correct 244 ms 12264 KB Output is correct
50 Correct 166 ms 10912 KB Output is correct
51 Correct 192 ms 11740 KB Output is correct
52 Correct 199 ms 12488 KB Output is correct
53 Correct 136 ms 10176 KB Output is correct
54 Correct 188 ms 11016 KB Output is correct
55 Correct 230 ms 11876 KB Output is correct
56 Correct 249 ms 12472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 472 KB Output is correct
31 Correct 2 ms 412 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 2 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 1 ms 476 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 480 KB Output is correct
39 Correct 2 ms 468 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 484 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 472 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 476 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 468 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 2 ms 468 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
55 Correct 124 ms 8776 KB Output is correct
56 Correct 177 ms 10016 KB Output is correct
57 Correct 125 ms 9780 KB Output is correct
58 Correct 109 ms 8668 KB Output is correct
59 Correct 159 ms 8716 KB Output is correct
60 Correct 189 ms 9492 KB Output is correct
61 Correct 145 ms 8684 KB Output is correct
62 Correct 164 ms 9736 KB Output is correct
63 Correct 112 ms 7940 KB Output is correct
64 Correct 163 ms 8896 KB Output is correct
65 Correct 200 ms 9732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 472 KB Output is correct
31 Correct 2 ms 412 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 2 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 1 ms 476 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 480 KB Output is correct
39 Correct 2 ms 468 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 484 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 472 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 476 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 468 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 2 ms 468 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
55 Correct 154 ms 10944 KB Output is correct
56 Correct 212 ms 11632 KB Output is correct
57 Correct 153 ms 12132 KB Output is correct
58 Correct 149 ms 11004 KB Output is correct
59 Correct 168 ms 10968 KB Output is correct
60 Correct 150 ms 9852 KB Output is correct
61 Correct 171 ms 10404 KB Output is correct
62 Correct 235 ms 11860 KB Output is correct
63 Correct 174 ms 10868 KB Output is correct
64 Correct 187 ms 11952 KB Output is correct
65 Correct 129 ms 9836 KB Output is correct
66 Correct 172 ms 10432 KB Output is correct
67 Correct 208 ms 11360 KB Output is correct
68 Correct 239 ms 12156 KB Output is correct
69 Correct 150 ms 11216 KB Output is correct
70 Correct 222 ms 12064 KB Output is correct
71 Correct 164 ms 12468 KB Output is correct
72 Correct 146 ms 11308 KB Output is correct
73 Correct 172 ms 11192 KB Output is correct
74 Correct 202 ms 11116 KB Output is correct
75 Correct 244 ms 12264 KB Output is correct
76 Correct 166 ms 10912 KB Output is correct
77 Correct 192 ms 11740 KB Output is correct
78 Correct 199 ms 12488 KB Output is correct
79 Correct 136 ms 10176 KB Output is correct
80 Correct 188 ms 11016 KB Output is correct
81 Correct 230 ms 11876 KB Output is correct
82 Correct 249 ms 12472 KB Output is correct
83 Correct 124 ms 8776 KB Output is correct
84 Correct 177 ms 10016 KB Output is correct
85 Correct 125 ms 9780 KB Output is correct
86 Correct 109 ms 8668 KB Output is correct
87 Correct 159 ms 8716 KB Output is correct
88 Correct 189 ms 9492 KB Output is correct
89 Correct 145 ms 8684 KB Output is correct
90 Correct 164 ms 9736 KB Output is correct
91 Correct 112 ms 7940 KB Output is correct
92 Correct 163 ms 8896 KB Output is correct
93 Correct 200 ms 9732 KB Output is correct
94 Correct 152 ms 11892 KB Output is correct
95 Correct 219 ms 13168 KB Output is correct
96 Correct 163 ms 13064 KB Output is correct
97 Correct 147 ms 11960 KB Output is correct
98 Correct 186 ms 11952 KB Output is correct
99 Correct 165 ms 11424 KB Output is correct
100 Correct 227 ms 13268 KB Output is correct
101 Correct 171 ms 11648 KB Output is correct
102 Correct 192 ms 12480 KB Output is correct
103 Correct 204 ms 13280 KB Output is correct
104 Correct 160 ms 11240 KB Output is correct
105 Correct 209 ms 12216 KB Output is correct
106 Correct 247 ms 13004 KB Output is correct
107 Correct 107 ms 13428 KB Output is correct
108 Correct 147 ms 11760 KB Output is correct
109 Correct 147 ms 11672 KB Output is correct
110 Correct 149 ms 11832 KB Output is correct
111 Correct 146 ms 11692 KB Output is correct
112 Correct 149 ms 11692 KB Output is correct
113 Correct 148 ms 11764 KB Output is correct
114 Correct 156 ms 11704 KB Output is correct
115 Correct 145 ms 11708 KB Output is correct
116 Correct 151 ms 11696 KB Output is correct