Submission #678059

# Submission time Handle Problem Language Result Execution time Memory
678059 2023-01-05T06:18:36 Z sunwukong123 Newspapers (CEOI21_newspapers) C++14
0 / 100
1 ms 348 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{"  << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach 
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first 
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> bool chmax(T& a, const T b) { return a<b ? a = b, 1 : 0; }
template <typename T> bool chmin(T& a, const T b) { return a>b ? a = b, 1 : 0; }
const int MAXN = -1;
vector<int> V[1005];
int n,m; 
int dist[1005]; 
pi bfs(int source) {
	memset(dist,-1,sizeof dist);
	queue<int> q;
	dist[source]=0;
	q.push(source);
	while(q.size()) {
		int x=q.front(); q.pop();
		for(auto v:V[x]){
			if(dist[v]==-1 || dist[v]>dist[x]+1) {
				dist[v]=dist[x]+1;
				q.push(v);
			}
		}
	}
	pi mx=pi(0,0);
	FOR(i,1,n) {
		chmax(mx,{dist[i],i});
	}
	return mx;
}
int dfs(int x, int p) {
	int l=1;
	for(auto v:V[x])if(v!=p) {
		int res=dfs(v,x);
		chmax(l,res+1);
	}
	return l;
}
int32_t main() 
{
	IAMSPEED
	cin >> n >> m;
	if(n==1) {
		return cout << "YES\n1 \n1", 0;
	} else if(n==2) {
		return cout << "YES\n2 \n1 1", 0;
	}
	if(m!=(n-1)) return cout << "NO\n", 0; // cycle=cannot
	/*
	cout << "YES\n";
	cout << 2*(n-2) << '\n';
	FOR(i,2,n-1) cout << i << ' ';
	DEC(i,n-1,2) cout << i << ' ';
	*/
	FOR(i,1,n-1){
		int a,b; cin >> a >> b;
		V[a].pb(b);
		V[b].pb(a);
	}
	pi r=bfs(1);
	int src=r.s;
	int sink=bfs(src).s;
	vector<int> path;
	path.pb(sink);
	while(path.back() != src) {
		int x=path.back();
		for(auto v:V[x]){
			if(dist[v]==dist[x]-1){
				path.pb(v);
				break;
			}
		}
	}
	int S=path.size();
	dbv(path);
	if(S==2||S==3) {
		return cout << "YES\n2 \n" << path[S/2] << ' ' << path[S/2], 0;
	}
	vector<int> ans;
	ans.pb(path[1]);
	FOR(i,2,path.size()-3) {
		int x=path[i];
		int pre=path[i-1];
		int nxt=path[i+1];
		vector<int> here;
		for(auto v:V[x]) {
			if(v==pre||v==nxt)continue;
			db(v);
			int l=dfs(v,x);
			if(l>2) {
				cout << "NO\n"; exit(0);
			}
			db(l);
			if(l==2) {
				here.pb(v);
			} // if not, either of the iterations will cover it anyways?
		}
		ans.pb(x);
		for(auto i:here) {
			ans.pb(i);
			ans.pb(x);
		}
		
	}
	ans.pb(path[S-2]);

	// now for the return. 
	ans.pb(path[S-2]);
	DEC(i,path.size()-3,2) {
		int x=path[i];
		int pre=path[i+1];
		int nxt=path[i-1];
		vector<int> here;
		for(auto v:V[x]) {
			if(v==pre||v==nxt)continue;
			int l=dfs(v,x);
			db(l);
			if(l>2) {
				cout << "NO\n"; exit(0);
			}
			if(l==2) {
				here.pb(v);
			} // if not, either of the iterations will cover it anyways?
		}
		ans.pb(x);
		for(auto i:here) {
			ans.pb(i);
			ans.pb(x);
		}
	}
	ans.pb(path[1]);
	cout << ans.size() << '\n';
	for(auto i:ans)cout<<i<<' ';
}


# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Incorrect 0 ms 340 KB Token "6" doesn't correspond to pattern "YES|NO"
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 Incorrect 1 ms 348 KB Token "4" doesn't correspond to pattern "YES|NO"
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Incorrect 0 ms 340 KB Token "6" doesn't correspond to pattern "YES|NO"
4 Halted 0 ms 0 KB -