Submission #55861

# Submission time Handle Problem Language Result Execution time Memory
55861 2018-07-09T06:16:29 Z Yehezkiel Pipes (CEOI15_pipes) C++11
70 / 100
3729 ms 19932 KB
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
typedef pair<int,int> pii;
const int batasMemori=360000;
const int MAXN=100000;
int n,m;
class UFDS{
	private:
	int par[MAXN+5];
	int finds(int u){
		if(par[u]!=u)
			par[u]=finds(par[u]);
		return par[u];
	}
	public:
	void inis(){
		for(int i=1;i<=n;i++)
			par[i]=i;
	}
	bool connected(int u,int v){
		return finds(u)==finds(v);
	}
	void gabung(int u,int v){
		u=finds(u);
		v=finds(v);
		if(u==v)
			return;
		par[u]=v;
	}
};
UFDS disjointSet;

const int logn=17;
vector <pii> pending,edgelist;
vector <pii> node[MAXN+5];
int kandungan[MAXN+5],par[logn][MAXN+5],delta[MAXN+5],depth[MAXN+5];
bool report[MAXN+5];

void dfs(int now,int _par,int _kandugan,int _depth){
	par[0][now]=_par;
	depth[now]=_depth;
	kandungan[now]=_kandugan;
	for(auto v:node[now])
		if(v.fi!=_par)
			dfs(v.fi,now,v.se,depth[now]+1);
}
void buildLCA(){
	for(int i=0;i<logn;i++)
		for(int j=1;j<=n;j++)
			par[i][j]=0;
	for(int i=1;i<=n;i++)
	{
		if(par[0][i]==0)
			dfs(i,0,-1,0);
	}
	for(int i=1;i<logn;i++)
	{
		for(int j=1;j<=n;j++)
			par[i][j]=par[i-1][par[i-1][j]];
	}
}
int lca(int u,int v){
	if(depth[u]>depth[v])
		swap(u,v);
	int beda=depth[v]-depth[u];
	for(int i=logn-1;i>=0;i--)
		if(beda&(1<<i))
			v=par[i][v];
	if(u==v)
		return u;
	for(int i=logn-1;i>=0;i--)
	{
		if(par[i][u]!=par[i][v])
		{
			u=par[i][u];
			v=par[i][v];
		}
	}
	return par[0][u];
}
int findReport(int now){
	int status=delta[now];
	for(auto v:node[now])
	{
		if(v.fi==par[0][now])
			continue;
		status+=findReport(v.fi);
	}
	if(status!=0&&kandungan[now]!=-1)				//part of cycle berarti
		report[kandungan[now]]=false;
	return status;
}
void doPending(){
	for(auto isi:pending)
	{
		int LCA=lca(isi.fi,isi.se);
		delta[LCA]-=2;
		delta[isi.fi]++;
		delta[isi.se]++;
	}
	for(int i=1;i<=n;i++)
	{
		if(par[0][i]==0)			//berarti root
			findReport(i);
	}
}
void solve(){
	for(int i=1;i<=n;i++)
		delta[i]=0;
	buildLCA();
	doPending();
}
void reset(){
	pending.clear();
}
int main()
{
	memset(report,true,sizeof(report));
	
	scanf("%d%d",&n,&m);
	disjointSet.inis();
	
	int u,v;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		if(disjointSet.connected(u,v))
			pending.eb(u,v);
		else
		{
			disjointSet.gabung(u,v);
			node[u].eb(v,edgelist.size());
			node[v].eb(u,edgelist.size());
			edgelist.eb(u,v);
		}
		
		if(i==m||i%batasMemori==0)
		{
			//do it
			solve();
			reset();
		}
	}
	for(int i=0;i<edgelist.size();i++)
	{
		if(!report[i])
			continue;
		printf("%d %d\n",edgelist[i].fi,edgelist[i].se);
	}
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:149:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<edgelist.size();i++)
              ~^~~~~~~~~~~~~~~~
pipes.cpp:125:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~
pipes.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&u,&v);
   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2944 KB Output is correct
2 Correct 4 ms 2904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3584 KB Output is correct
2 Correct 9 ms 3584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 7188 KB Output is correct
2 Correct 179 ms 7140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 370 ms 7496 KB Output is correct
2 Correct 424 ms 7584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 641 ms 8776 KB Output is correct
2 Correct 538 ms 9352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 980 ms 14604 KB Output is correct
2 Correct 839 ms 14500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1767 ms 16288 KB Output is correct
2 Correct 1491 ms 15664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2547 ms 19920 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3218 ms 19932 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3729 ms 17964 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -