답안 #519363

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519363 2022-01-26T09:04:21 Z Keshi Newspapers (CEOI21_newspapers) C++17
100 / 100
56 ms 96076 KB
//In the name of God
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll maxn = 2e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()

ll pw(ll a, ll b){
	ll c = 1;
	while(b){
		if(b & 1) c = c * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return c;
}

int n, m;
namespace sub1{
int g[maxn], d[maxn], p[maxn], pp[maxn];
bool vis[maxn];
queue<int> q;

int get(int x){
	int y = 0;
	for(int i = 0; i < n; i++){
		if((x >> i) & 1) y |= g[i];
	}
	return y;
}
bool check(vector<int> vec){
	int v = (1 << n) - 1;
	for(int i : vec){
		v = get(v & (~(1 << i)));

		cout << i << "  ! ";
		for(int j = 0; j < n; j++){
			if((v >> j) & 1) cout << j << " ";
			else cout << "  ";
		}
		cout << "\n";
	}
	return (v == 0);
}

void solve(){

	for(int i = 0; i < m; i++){
		int v, u;
		cin >> v >> u;
		v--;
		u--;
		g[v] |= (1 << u);
		g[u] |= (1 << v);
	}
	p[(1 << n) - 1] = -1;
	vis[(1 << n) - 1] = 1;
	q.push((1 << n) - 1);
	while(!q.empty()){
		int v = q.front();
		q.pop();
		for(int i = 0; i < n; i++){
			int u = get(v & (~(1 << i)));
			if(!vis[u]){
				vis[u] = 1;
				d[u] = d[v] + 1;
				p[u] = v;
				pp[u] = i;
				q.push(u);
			}
		}
	}
	if(!vis[0]){
		cout << "NO\n";
		return;
	}
	cout << "YES\n";
	cout << d[0] << "\n";
	int v = 0;
	vector<int> vec;
	while(p[v] != -1){
		vec.pb(pp[v]);
		v = p[v];
	}
	reverse(all(vec));
	for(int i : vec){
		cout << i + 1 << " ";
	}
	return;
}
};

vector<int> gg[maxn], g[maxn], vec;
bool vvv;

void dfs(int v, int p = -1){
	if(vvv) vec.pb(v);
	vvv = 1;
	for(int u : g[v]){
		if(Sz(g[u]) == 1){
			vec.pb(u);
			vec.pb(v);
		}
	}
	for(int u : g[v]){
		if(Sz(g[u]) != 1 && u != p) dfs(u, v);
	}
	return;
}

int main(){
	fast_io;

	cin >> n >> m;
	if(m != n - 1){
		cout << "NO\n";
		return 0;
	}
	if(n <= 20){
		sub1::solve();
		return 0;
	}
	for(int i = 0; i < m; i++){
		int v, u;
		cin >> v >> u;
		gg[v].pb(u);
		gg[u].pb(v);
	}
	vector<int> ve2;
	for(int i = 1; i <= n; i++){
		if(Sz(gg[i]) == 1) continue;
		ve2.pb(i);
		for(int j : gg[i]){
			if(Sz(gg[j]) != 1) g[i].pb(j);
		}
	}
	if(Sz(ve2) == 1){
		cout << "YES\n2\n" << ve2[0] << " " << ve2[0] << "\n";
		return 0;
	}
	if(Sz(ve2) == 2){
		cout << "YES\n2\n" << ve2[0] << " " << ve2[1] << " " << ve2[1] << " " << ve2[0] << "\n";
		return 0;
	}
	int v = -1;
	for(int i = 1; i <= n; i++){
		if(Sz(g[i]) < 2) continue;
		int c = 0;
		for(int j : g[i]){
			if(Sz(g[j]) != 1) c++;
		}
		if(c > 2){
			cout << "NO\n";
			return 0;
		}
		if(c != 2) v = i;
	}
	dfs(v);
	vec.pop_back();
	vector<int> v2 = vec;
	reverse(all(v2));
	for(int i : v2){
		vec.pb(i);
	}
	cout << "YES\n";
	cout << Sz(vec) << "\n";
	for(int i : vec){
		cout << i << " ";
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 94148 KB Output is correct
2 Correct 42 ms 94164 KB Output is correct
3 Correct 43 ms 94244 KB Output is correct
4 Correct 43 ms 94188 KB Output is correct
5 Correct 42 ms 94148 KB Output is correct
6 Correct 43 ms 94160 KB Output is correct
7 Correct 43 ms 94120 KB Output is correct
8 Correct 42 ms 94268 KB Output is correct
9 Correct 42 ms 94276 KB Output is correct
10 Correct 45 ms 94164 KB Output is correct
11 Correct 48 ms 94288 KB Output is correct
12 Correct 45 ms 94276 KB Output is correct
13 Correct 53 ms 94272 KB Output is correct
14 Correct 49 ms 94212 KB Output is correct
15 Correct 41 ms 94276 KB Output is correct
16 Correct 42 ms 94228 KB Output is correct
17 Correct 42 ms 94316 KB Output is correct
18 Correct 46 ms 94316 KB Output is correct
19 Correct 49 ms 94192 KB Output is correct
20 Correct 48 ms 94280 KB Output is correct
21 Correct 45 ms 94236 KB Output is correct
22 Correct 46 ms 94184 KB Output is correct
23 Correct 45 ms 94244 KB Output is correct
24 Correct 43 ms 94344 KB Output is correct
25 Correct 43 ms 94252 KB Output is correct
26 Correct 43 ms 94304 KB Output is correct
27 Correct 47 ms 94324 KB Output is correct
28 Correct 45 ms 94360 KB Output is correct
29 Correct 47 ms 94364 KB Output is correct
30 Correct 45 ms 94364 KB Output is correct
31 Correct 46 ms 94404 KB Output is correct
32 Correct 43 ms 94380 KB Output is correct
33 Correct 45 ms 94368 KB Output is correct
34 Correct 44 ms 94392 KB Output is correct
35 Correct 42 ms 94660 KB Output is correct
36 Correct 43 ms 94632 KB Output is correct
37 Correct 45 ms 94616 KB Output is correct
38 Correct 44 ms 94660 KB Output is correct
39 Correct 45 ms 94880 KB Output is correct
40 Correct 43 ms 94944 KB Output is correct
41 Correct 46 ms 94528 KB Output is correct
42 Correct 46 ms 94916 KB Output is correct
43 Correct 44 ms 94948 KB Output is correct
44 Correct 43 ms 94864 KB Output is correct
45 Correct 43 ms 94796 KB Output is correct
46 Correct 51 ms 94652 KB Output is correct
47 Correct 45 ms 95164 KB Output is correct
48 Correct 48 ms 95684 KB Output is correct
49 Correct 47 ms 94712 KB Output is correct
50 Correct 44 ms 94852 KB Output is correct
51 Correct 44 ms 95560 KB Output is correct
52 Correct 43 ms 94592 KB Output is correct
53 Correct 44 ms 94756 KB Output is correct
54 Correct 44 ms 95312 KB Output is correct
55 Correct 46 ms 95088 KB Output is correct
56 Correct 43 ms 94552 KB Output is correct
57 Correct 44 ms 96076 KB Output is correct
58 Correct 45 ms 94916 KB Output is correct
59 Correct 43 ms 94236 KB Output is correct
60 Correct 42 ms 94148 KB Output is correct
61 Correct 44 ms 94160 KB Output is correct
62 Correct 46 ms 94216 KB Output is correct
63 Correct 44 ms 94228 KB Output is correct
64 Correct 44 ms 94284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 94168 KB Output is correct
2 Correct 45 ms 94208 KB Output is correct
3 Correct 44 ms 94248 KB Output is correct
4 Correct 47 ms 94372 KB Output is correct
5 Correct 43 ms 94232 KB Output is correct
6 Correct 44 ms 94148 KB Output is correct
7 Correct 43 ms 94156 KB Output is correct
8 Correct 44 ms 94260 KB Output is correct
9 Correct 42 ms 94176 KB Output is correct
10 Correct 45 ms 94428 KB Output is correct
11 Correct 45 ms 94412 KB Output is correct
12 Correct 49 ms 94332 KB Output is correct
13 Correct 45 ms 94248 KB Output is correct
14 Correct 51 ms 94356 KB Output is correct
15 Correct 44 ms 94264 KB Output is correct
16 Correct 55 ms 94408 KB Output is correct
17 Correct 46 ms 94352 KB Output is correct
18 Correct 45 ms 94408 KB Output is correct
19 Correct 45 ms 94404 KB Output is correct
20 Correct 44 ms 94404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 94148 KB Output is correct
2 Correct 42 ms 94164 KB Output is correct
3 Correct 43 ms 94244 KB Output is correct
4 Correct 43 ms 94188 KB Output is correct
5 Correct 42 ms 94148 KB Output is correct
6 Correct 43 ms 94160 KB Output is correct
7 Correct 43 ms 94120 KB Output is correct
8 Correct 42 ms 94268 KB Output is correct
9 Correct 42 ms 94276 KB Output is correct
10 Correct 45 ms 94164 KB Output is correct
11 Correct 48 ms 94288 KB Output is correct
12 Correct 45 ms 94276 KB Output is correct
13 Correct 53 ms 94272 KB Output is correct
14 Correct 49 ms 94212 KB Output is correct
15 Correct 41 ms 94276 KB Output is correct
16 Correct 42 ms 94228 KB Output is correct
17 Correct 42 ms 94316 KB Output is correct
18 Correct 46 ms 94316 KB Output is correct
19 Correct 49 ms 94192 KB Output is correct
20 Correct 48 ms 94280 KB Output is correct
21 Correct 45 ms 94236 KB Output is correct
22 Correct 46 ms 94184 KB Output is correct
23 Correct 45 ms 94244 KB Output is correct
24 Correct 43 ms 94344 KB Output is correct
25 Correct 43 ms 94252 KB Output is correct
26 Correct 43 ms 94304 KB Output is correct
27 Correct 47 ms 94324 KB Output is correct
28 Correct 45 ms 94360 KB Output is correct
29 Correct 47 ms 94364 KB Output is correct
30 Correct 45 ms 94364 KB Output is correct
31 Correct 46 ms 94404 KB Output is correct
32 Correct 43 ms 94380 KB Output is correct
33 Correct 45 ms 94368 KB Output is correct
34 Correct 44 ms 94392 KB Output is correct
35 Correct 42 ms 94660 KB Output is correct
36 Correct 43 ms 94632 KB Output is correct
37 Correct 45 ms 94616 KB Output is correct
38 Correct 44 ms 94660 KB Output is correct
39 Correct 45 ms 94880 KB Output is correct
40 Correct 43 ms 94944 KB Output is correct
41 Correct 46 ms 94528 KB Output is correct
42 Correct 46 ms 94916 KB Output is correct
43 Correct 44 ms 94948 KB Output is correct
44 Correct 43 ms 94864 KB Output is correct
45 Correct 43 ms 94796 KB Output is correct
46 Correct 51 ms 94652 KB Output is correct
47 Correct 45 ms 95164 KB Output is correct
48 Correct 48 ms 95684 KB Output is correct
49 Correct 47 ms 94712 KB Output is correct
50 Correct 44 ms 94852 KB Output is correct
51 Correct 44 ms 95560 KB Output is correct
52 Correct 43 ms 94592 KB Output is correct
53 Correct 44 ms 94756 KB Output is correct
54 Correct 44 ms 95312 KB Output is correct
55 Correct 46 ms 95088 KB Output is correct
56 Correct 43 ms 94552 KB Output is correct
57 Correct 44 ms 96076 KB Output is correct
58 Correct 45 ms 94916 KB Output is correct
59 Correct 43 ms 94236 KB Output is correct
60 Correct 42 ms 94148 KB Output is correct
61 Correct 44 ms 94160 KB Output is correct
62 Correct 46 ms 94216 KB Output is correct
63 Correct 44 ms 94228 KB Output is correct
64 Correct 44 ms 94284 KB Output is correct
65 Correct 45 ms 94204 KB Output is correct
66 Correct 52 ms 94272 KB Output is correct
67 Correct 49 ms 94244 KB Output is correct
68 Correct 45 ms 94256 KB Output is correct
69 Correct 45 ms 94240 KB Output is correct
70 Correct 44 ms 94240 KB Output is correct
71 Correct 44 ms 94156 KB Output is correct
72 Correct 45 ms 94256 KB Output is correct
73 Correct 45 ms 94276 KB Output is correct
74 Correct 45 ms 94204 KB Output is correct
75 Correct 47 ms 94152 KB Output is correct
76 Correct 45 ms 94236 KB Output is correct
77 Correct 46 ms 94236 KB Output is correct
78 Correct 45 ms 94148 KB Output is correct
79 Correct 45 ms 94296 KB Output is correct
80 Correct 50 ms 94364 KB Output is correct
81 Correct 46 ms 94236 KB Output is correct
82 Correct 48 ms 94176 KB Output is correct
83 Correct 49 ms 94252 KB Output is correct
84 Correct 45 ms 94244 KB Output is correct
85 Correct 45 ms 94252 KB Output is correct
86 Correct 47 ms 94280 KB Output is correct
87 Correct 48 ms 94300 KB Output is correct
88 Correct 47 ms 94292 KB Output is correct
89 Correct 46 ms 94392 KB Output is correct
90 Correct 51 ms 94280 KB Output is correct
91 Correct 54 ms 94324 KB Output is correct
92 Correct 46 ms 94340 KB Output is correct
93 Correct 46 ms 94268 KB Output is correct
94 Correct 48 ms 94244 KB Output is correct
95 Correct 45 ms 94356 KB Output is correct
96 Correct 45 ms 94364 KB Output is correct
97 Correct 46 ms 94288 KB Output is correct
98 Correct 46 ms 94376 KB Output is correct
99 Correct 45 ms 94320 KB Output is correct
100 Correct 49 ms 94324 KB Output is correct
101 Correct 45 ms 94276 KB Output is correct
102 Correct 48 ms 94256 KB Output is correct
103 Correct 45 ms 94276 KB Output is correct
104 Correct 45 ms 94336 KB Output is correct
105 Correct 44 ms 94284 KB Output is correct
106 Correct 45 ms 94276 KB Output is correct
107 Correct 46 ms 94280 KB Output is correct
108 Correct 45 ms 94356 KB Output is correct
109 Correct 51 ms 94272 KB Output is correct
110 Correct 45 ms 94276 KB Output is correct
111 Correct 44 ms 94304 KB Output is correct
112 Correct 56 ms 94336 KB Output is correct
113 Correct 45 ms 94364 KB Output is correct
114 Correct 49 ms 94284 KB Output is correct
115 Correct 43 ms 94276 KB Output is correct
116 Correct 46 ms 94252 KB Output is correct
117 Correct 46 ms 94276 KB Output is correct
118 Correct 44 ms 94264 KB Output is correct
119 Correct 50 ms 94280 KB Output is correct
120 Correct 45 ms 94352 KB Output is correct
121 Correct 44 ms 94276 KB Output is correct
122 Correct 45 ms 94288 KB Output is correct
123 Correct 45 ms 94256 KB Output is correct
124 Correct 45 ms 94284 KB Output is correct
125 Correct 50 ms 94260 KB Output is correct
126 Correct 45 ms 94276 KB Output is correct
127 Correct 45 ms 94228 KB Output is correct
128 Correct 53 ms 94276 KB Output is correct
129 Correct 44 ms 94220 KB Output is correct
130 Correct 45 ms 94308 KB Output is correct
131 Correct 47 ms 94252 KB Output is correct
132 Correct 46 ms 94372 KB Output is correct
133 Correct 46 ms 94392 KB Output is correct
134 Correct 45 ms 94216 KB Output is correct
135 Correct 44 ms 94284 KB Output is correct
136 Correct 45 ms 94276 KB Output is correct
137 Correct 46 ms 94224 KB Output is correct
138 Correct 45 ms 94176 KB Output is correct
139 Correct 44 ms 94272 KB Output is correct
140 Correct 43 ms 94192 KB Output is correct
141 Correct 44 ms 94212 KB Output is correct
142 Correct 44 ms 94260 KB Output is correct
143 Correct 43 ms 94252 KB Output is correct
144 Correct 55 ms 94504 KB Output is correct
145 Correct 47 ms 94408 KB Output is correct