Submission #59215

# Submission time Handle Problem Language Result Execution time Memory
59215 2018-07-21T07:19:05 Z gnoor Islands (IOI08_islands) C++17
12 / 100
1099 ms 132096 KB
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

struct edge {
	int to,wgt;
	int id;
};

vector<edge> pth[1000100];
bool visit[1000100];
bool incyc[1000100];

int cyc[2000100];
long long wgt[2000100];
long long dis[2000100];
int cn;

int dfs(int x,int p) {
	visit[x]=true;
	int ret=0;
	bool toret=false;
	for (auto i:pth[x]) {
		if (i.id==p) continue;
		if (visit[i.to]) {
			if (toret) continue;
			cyc[cn]=x;
			wgt[cn]=i.wgt;
			incyc[x]=true;
			cn++;
			ret=i.to;
			toret=true;
			continue;
		}
		int res=dfs(i.to,i.id);
		if (toret) continue;
		if (res) {
			cyc[cn]=x;
			wgt[cn]=i.wgt;
			incyc[x]=true;
			cn++;
			if (res!=x) ret=res;
			else ret=0;
			toret=true;
		}
	}
	return ret;
}

long long dmax[1000100];
long long survey(int x,int p) {
	long long res=0;
	long long mx=0;
	long long smx=0;
	long long cur;
	dmax[x]=0;
	for (auto i:pth[x]) {
		if (i.id==p) continue;
		if (incyc[i.to]) continue;
		cur=i.wgt+survey(i.to,i.id);
		if (cur>=mx) {
			smx=mx;
			mx=cur;
		} else if (cur>smx) {
			smx=cur;
		}
		res=max(res,i.wgt+cur);
		dmax[x]=max(dmax[x],dmax[i.to]);
	}
	dmax[x]=max(dmax[x],mx+smx);
	return res;
}

int main () {
	int n;
	scanf("%d",&n);
	int a,b;
	for (int i=1;i<=n;i++) {
		scanf("%d%d",&a,&b);
		pth[i].push_back(edge{a,b,i});
		pth[a].push_back(edge{i,b,i});
	}
	long long ans=0;
	for (int i=1;i<=n;i++) {
		if (visit[i]) continue;
		cn=1;
		dfs(i,0);
		cn--;
		for (int j=1;j<=cn;j++) {
			dis[j]=survey(cyc[j],0);	
			dis[j+cn]=dis[j];
			wgt[j+cn]=wgt[j];
			cyc[j+cn]=cyc[j];
		}
		wgt[0]=0;
		for (int j=1;j<=cn*2;j++) {
			wgt[j]+=wgt[j-1];
		}	
		deque<int> qq;
		qq.push_back(1);
		long long cur=0;
		for (int j=2;j<=cn*2;j++) {
			while (!qq.empty()&&qq.front()<=j-cn) qq.pop_front();
			cur=max(cur,wgt[j]+dis[j]+dis[qq.front()]-wgt[qq.front()]);
			cur=max(cur,dmax[cyc[j]]);

			while (!qq.empty()&&dis[qq.back()]-wgt[qq.back()]<=dis[j]-wgt[j]) qq.pop_back();
			qq.push_back(j);
		}
		ans+=cur;
	}
	printf("%lld\n",ans);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
islands.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&a,&b);
   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23800 KB Output is correct
2 Incorrect 22 ms 23920 KB Output isn't correct
3 Correct 23 ms 24000 KB Output is correct
4 Incorrect 27 ms 24008 KB Output isn't correct
5 Correct 29 ms 24088 KB Output is correct
6 Incorrect 24 ms 24088 KB Output isn't correct
7 Incorrect 22 ms 24088 KB Output isn't correct
8 Incorrect 27 ms 24156 KB Output isn't correct
9 Incorrect 23 ms 24156 KB Output isn't correct
10 Incorrect 23 ms 24156 KB Output isn't correct
11 Correct 21 ms 24156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 24344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 24344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 40 ms 25412 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 79 ms 30060 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 167 ms 40096 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 261 ms 54508 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 496 ms 96780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1099 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
2 Halted 0 ms 0 KB -