Submission #764669

# Submission time Handle Problem Language Result Execution time Memory
764669 2023-06-23T20:45:56 Z pere_gil Logičari (COCI21_logicari) C++14
10 / 110
90 ms 28056 KB
#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
#define ii pair<int,int>
#define vi vector<int>
#define all(x) x.begin(),x.end()

const int MAX_N=2e5+5;
vi adj[MAX_N];
set<int> dis[MAX_N];

int main(){
	int n; scanf("%d",&n);
	for(int i=0;i<n;i++){
		int u,v; scanf("%d %d",&u,&v);
		u--,v--;
		dis[u].insert(v);
		dis[v].insert(u);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	bool sw=true;
	for(int i=0;i<n;i++)
		sw&=adj[i].size()==2;

	if(sw){
		printf("%d\n",n%4 ? -1 : n/2);
		return 0;
	}

	bool good[n]={},in[n]={},taken[n]={};
	
	while(true){
		int act=-1;
		for(int i=0;i<n;i++)
			if(!good[i]){
				if(!dis[i].size()){
					printf("-1\n");
					return 0;
				}
				else if(dis[i].size()==1) act=i;
			}
		
		if(act==-1) break;

		act=*dis[act].begin();

		taken[act]=in[act]=true;

		for(int u: adj[act]) good[u]=true;
		set<int> aux;
		for(int u: adj[act])
			for(int v: adj[u]){
				if(taken[v]) continue;
				taken[v]=true;
				aux.insert(v);
			}

		for(int i=0;i<n;i++)
			for(int x: aux)
				dis[i].erase(x);
	}
	
	int res=0;
	for(int i=0;i<n;i++) res+=in[i];
	printf("%d\n",res);
	
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:16:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |  int n; scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
Main.cpp:18:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   int u,v; scanf("%d %d",&u,&v);
      |            ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14292 KB Output is correct
2 Correct 9 ms 14292 KB Output is correct
3 Correct 8 ms 14388 KB Output is correct
4 Correct 7 ms 14356 KB Output is correct
5 Correct 90 ms 27964 KB Output is correct
6 Correct 90 ms 28056 KB Output is correct
7 Correct 87 ms 27972 KB Output is correct
8 Correct 82 ms 27952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 14292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 14292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14292 KB Output is correct
2 Correct 9 ms 14292 KB Output is correct
3 Correct 8 ms 14388 KB Output is correct
4 Correct 7 ms 14356 KB Output is correct
5 Correct 90 ms 27964 KB Output is correct
6 Correct 90 ms 28056 KB Output is correct
7 Correct 87 ms 27972 KB Output is correct
8 Correct 82 ms 27952 KB Output is correct
9 Incorrect 7 ms 14292 KB Output isn't correct
10 Halted 0 ms 0 KB -