This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
#include<bits/stdc++.h>
/**/
//typedef int ll;
typedef long long ll;
typedef unsigned long long ull;
typedef std::string str;
/*typedef __int128 vll;
typedef unsigned __int128 uvll;*/
#define llll std::pair<ll , ll>
#define pb push_back
#define pf push_front
#define halo cout << "hello\n"
#define fi first
#define sec second
#define all(a) a.begin() , a.end()
const ll limit = 998244353;
const ll ous = 1e5 + 7;
const ll dx[4] = {-1 , 0 , 1 , 0} , dy[4] = {0,1,0,-1};
std::vector<ll> ar , adj[ous] , vis(ous , 0) , crcol(ous , 0);
ll n , m;
ll f = 0 , s = 0;
ll dfs(ll crt , ll prv){
vis[crt] = 1;
for(ll j= 0;adj[crt].size() > j;j++){
ll pt = adj[crt][j];
if(pt != prv){
if(vis[pt] == 1){
f = crt ; s = pt;
adj[crt].erase(adj[crt].begin() + j);
for(ll i = 0;adj[j].size() > i;i++){
if(adj[j][i] == crt){
adj[j].erase(adj[j].begin() + i);
break;
}
}
return 1;
}
if(dfs(pt , crt)){
return 1;
}
}
}
return 0;
}
ll sdfs (ll col , ll crt , ll prv){
if(crcol[crt] != 0){
if(crcol[crt] == 1 && (col == 2 || col == 3)){
return -1;
}
if(crcol[crt] == 2 && (col == 1 || col == 4)){
return -1;
}
if(crcol[crt] == 3 && (col == 3 || col == 1 || col == 4)){
return -1;
}
if(crcol[crt] == 4 && (col == 3 || col == 4 || col == 2)){
return -1;
}
if(crcol[crt] == 3 && col == 2){
col = 3;
}
if(crcol[crt] == 4 && col == 1){
col = 4;
}
}
ll kd = 1;
ll sm = (col == 2 || col == 3);
for(auto j:adj[crt]){
if(j != prv){
kd = 0;
//std::cout << (col % 4) + 1 << " " << j << " " << crt << std::endl;
ll t = sdfs((col % 4) + 1 , j , crt);
if(t == -1){
return -1;
}
sm += t;
}
}
if(kd && (col == 1 || col == 2)){
return -1;
}
return sm;
}
void solve(){
std::cin >> n;
for(ll i = 0;n>i;i++){
ll x , y;std::cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1 , 0);
ll ans = limit;
crcol[f] = 1;crcol[s] = 1;
ll t = sdfs(1 , f , 0);
if(t != -1){
ans = std::min(ans , t);
}
//std::cout << "\n";
crcol[f] = 2;crcol[s] = 4;
t = sdfs(2 , f , 0);
if(t != -1){
ans = std::min(ans , t);
}
//std::cout << "\n";
crcol[f] = 4;crcol[s] = 2;
t = sdfs(1 , f , 0);
if(t != -1){
ans = std::min(ans , t);
}
//std::cout << "\n";
crcol[f] = 3;crcol[s] = 3;
t = sdfs(2 , f , 0);
if(t != -1){
ans = std::min(ans , t);
}
//std::cout << "\n";
if(ans == limit){
std::cout << "-1\n";
return;
}
//std::cout << "\n";
std::cout << ans << "\n";
//std::cout <<f << " " << s << "\n";
return; /**/
}
signed main(){
std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
ll t=1;
//std::cin >> t;
ll o = 1;
while(t--){
//cout << "Case " << o++ << ":\n";
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp:5:78: warning: "/*" within comment [-Wcomment]
5 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
|
Main.cpp: In function 'll dfs(ll, ll)':
Main.cpp:29:33: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
29 | for(ll j= 0;adj[crt].size() > j;j++){
| ~~~~~~~~~~~~~~~~^~~
Main.cpp:35:44: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
35 | for(ll i = 0;adj[j].size() > i;i++){
| ~~~~~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:135:8: warning: unused variable 'o' [-Wunused-variable]
135 | ll o = 1;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |