QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#104103#6406. Stage ClearHe_RenWA 2ms3868kbC++142.4kb2023-05-08 17:10:342023-05-08 17:10:35

Judging History

This is the latest submission verdict.

  • [2024-08-15 21:05:17]
  • hack成功,自动添加数据
  • (/hack/778)
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-08 17:10:35]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 3868kb
  • [2023-05-08 17:10:34]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 72 + 5;
const ll linf = 0x3f3f3f3f3f3f3f3f;

#define bbit(i) (1<<(i))
#define bdig(x,i) (((x)>>(i))&1)

struct Data
{
	ll a,b;
	Data(void){}
	Data(ll _a,ll _b): a(_a), b(_b) {}
	int gettype(void) const { return a <= b;}
};
bool operator < (const Data &A,const Data &B)
{
	int tA = A.gettype(), tB = B.gettype();
	if(tA != tB) return tA > tB;
	return tA? A.a < B.a: A.b > B.b;
}
Data operator + (const Data &A,const Data &B)
{
	ll sum = - A.a + A.b - B.a + B.b;
	ll mn = min(- A.a, - A.a + A.b - B.a);
	return Data(-mn, sum - mn);
}

int n,m;
Data p[MAXN];
vector<int> g[MAXN];

namespace P1
{
	const int ALL = (1<<25) + 5;
	
	int gmask[MAXN];
	Data f[ALL];
	
	ll solve(void)
	{
		int all = (1<<(n-1)) - 1;
		
		for(int u=1; u<=n; ++u)
			for(int v: g[u])
				gmask[u] |= bbit(v-1);
		
		fill(f, f+all+1, Data(linf, -linf));
		f[0] = Data(0,0);
		for(int mask=0; mask<=all; ++mask) if(f[mask].a != linf)
		{
			for(int v=2; v<=n; ++v)
				if(!bdig(mask, v-2) && (((mask << 1) | 1) & gmask[v]) != 0)
				{
					int nxt = mask | bbit(v-2);
					f[nxt] = min(f[nxt], f[mask] + p[v]);
				}
		}
		
		return f[all].a;
	}
}

namespace P2
{
	int anc[MAXN];
	
	int fa[MAXN];
	Data val[MAXN];
	int find(int u){ return fa[u] == u? u: fa[u] = find(fa[u]);}
	
	Data res;
	
	void dfs(int dep)
	{
		if(dep > n)
		{
			set< pair<Data,int> > q;
			iota(fa+1, fa+n+1, 1);
			for(int i=1; i<=n; ++i)
			{
				val[i] = p[i];
				if(i > 1) q.emplace(val[i], i);
			}
			
			while(q.size())
			{
				int u = q.begin() -> second; q.erase(q.begin());
				int v = find(anc[u]);
				if(v > 1) q.erase({val[v], v});
				
				fa[u] = v;
				val[v] = val[v] + val[u];
				
				if(v > 1) q.emplace(val[v], v);
			}
			
			res = min(res, val[1]);
			
			return;
		}
		for(int v: g[dep])
		{
			anc[dep] = v;
			dfs(dep+1);
		}
	}
	
	ll solve(void)
	{
		res = Data(linf, -linf);
		dfs(2);
		return res.a;
	}
}

int main(void)
{
	scanf("%d%d",&n,&m);
	for(int i=2; i<=n; ++i)
		scanf("%lld%lld",&p[i].a,&p[i].b);
	for(int i=1; i<=m; ++i)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		g[v].emplace_back(u);
	}
	
	ll ans;
	if(n <= 25)
		ans = P1 :: solve();
	else
		ans = P2 :: solve();
	
	printf("%lld\n",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3612kb

input:

4 4
4 2
5 3
2 6
1 2
1 3
2 4
3 4

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3868kb

input:

15 14
254040392438309 117083115436273
500005748229691 557255157630172
821034233718230 865199673774998
659892147898798 987564141425694
81172575487567 811635577877255
751768357864605 341103322647288
454926350150218 140191090713900
921608121471585 659295670987251
223751724062143 505619245326640
8907765...

output:

2885289598874333

result:

wrong answer 1st numbers differ - expected: '1665396301509143', found: '2885289598874333'