이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
vi g[1001];
vi d(1001), mxd(1001), p(1001);
// mxd[u] - max d[v] for v in subtree u
void dfs(int u){
mxd[u] = d[u];
for (int v : g[u]){
if (p[u]==v) continue;
p[v] = u; d[v]=d[u]+1;
dfs(v);
mxd[u] = max(mxd[u], mxd[v]);
}
}
void solve(){
int n, m; cin >> n >> m;
FOR(i,0,m){
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
if (m>n-1){
cout << "NO\n";
return;
}
int a=1, b=1; // longest path
int mx = 0;
FOR(u,1,n+1){
FOR(i,1,n+1){
d[i]=0; p[i]=0; mxd[i]=0;
}
dfs(u);
if (mxd[u] > mx){
a = u; mx = mxd[u];
FOR(v,1,n+1){
if (d[v] == mxd[u]){
b = v; break;
}
}
}
int cnt = 0;
for (int v : g[u]){
if (mxd[v] >= 3){
cnt++;
}
}
if (cnt >= 3){
cout << "NO\n";
return;
}
}
cout << "YES\n";
if (n==1){
cout << 1 << '\n' << 1 << '\n';
return;
}
if (n==2){
cout << 2 << '\n' << 1 << " " << 1 << '\n';
return;
}
FOR(i,1,n+1){
d[i]=0; p[i]=0; mxd[i]=0;
}
dfs(a);
vi ans;
while (p[b] != a){
int c = p[b];
ans.pb(c);
for (int u : g[c]){
if (u == b || p[c] == u) continue;
if (g[u].size()>=2){
ans.pb(u);
ans.pb(c);
}
}
b = c;
}
cout << 2*ans.size() << '\n';
FOR(i,0,n) cout << ans[i] << " ";
ROF(i,n-1,0) cout << ans[i] << " ";
cout << '\n';
}
int main(){
ios::sync_with_stdio(false);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
newspapers.cpp: In function 'int main()':
newspapers.cpp:103:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
newspapers.cpp:104:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |