QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#760113 | #4246. Cactus is Money | Wuyanru | WA | 2ms | 10208kb | C++14 | 4.4kb | 2024-11-18 14:51:04 | 2024-11-18 14:51:04 |
Judging History
answer
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
using ll=long long;
using ld=long double;
using pli=pair<ll,int>;
using pi=pair<int,int>;
template<typename A>
using vc=vector<A>;
using pl=pair<ll,ll>;
inline int read()
{
int s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline ll lread()
{
ll s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
template<const int N,const int M>
struct graph
{
int head[N+5];
int ww[M+5];
int t[M+5];
int x[M+5];
int cntm;
graph(){ cntm=1;}
inline void clear(int n=N)
{
cntm=1;
for(int i=1;i<=n;i++) head[i]=0;
}
inline void ad(int u,int v,int w=0)
{
cntm++;
t[cntm]=v;
x[cntm]=head[u];
ww[cntm]=w;
head[u]=cntm;
}
inline void add(int u,int v,int w=0)
{
ad(u,v,w);
ad(v,u,w);
}
inline int st(int num){ return head[num];}
inline int to(int num){ return t[num];}
inline int nx(int num){ return x[num];}
inline int w(int num){ return ww[num];}
};
graph<50000,100000>g;
priority_queue<pi,vc<pi>,greater<pi> >que;
vc<pl>nod[200005];
int vis[100005];
int u[100005];
int v[100005];
int a[100005];
int b[100005];
int dep[50005];
int fa[50005];
int faw[50005];
int f[50005];
int n,m,c;
inline int find(int num)
{
if(f[num]==num) return num;
return f[num]=find(f[num]);
}
void dfs(int num)
{
for(int i=g.st(num);i;i=g.nx(i))
{
int p=g.to(i);
if(p==fa[num]){ faw[num]=p;continue;}
fa[p]=num,dep[p]=dep[num]+1,dfs(p);
}
}
inline void run(int e)
{
vc<pl>edge;ll sa=a[e],sb=b[e];
edge.push_back(pl(a[e],b[e]));
int u=::u[e],v=::v[e];
// printf("e=%d : %d %d\n",e,u,v);
while(u!=v)
{
if(dep[u]<dep[v]) swap(u,v);
int id=faw[u];u=fa[u];vis[id]=2;
edge.push_back(pl(a[id],b[id]));
sa+=a[id],sb+=b[id];
}
for(auto &i:edge) i.first=sa-i.first,i.second=sb-i.second;
sort(edge.begin(),edge.end(),[](pl a,pl b)
{
if(a.first!=b.first) return a.first<b.first;
return a.second>b.second;
});
c++;
for(auto i:edge)
{
while(nod[c].size()>=2)
{
auto p=nod[c].back(),q=nod[c].end()[-2];
// q p i
if((p.second-q.second)*(i.first-p.first)>(i.second-p.second)*(p.first-q.first)) nod[c].pop_back();
else break;
}
nod[c].push_back(i);
}
que.push(pi(nod[c].size(),c));
}
inline bool check(int u,unsigned x,int v,unsigned y)
{
if(x+1>=nod[u].size()) return false;
if(y+1>=nod[v].size()) return true;
int x1,x2,x3,x4,y1,y2,y3,y4;
tie(x1,y1)=nod[u][x],tie(x2,y2)=nod[u][x+1];
tie(x3,y3)=nod[v][y],tie(x4,y4)=nod[v][y+1];
return (y2-y1)*(x4-x3)<(y4-y3)*(x2-x1);
}
inline void merge(int u,int v)
{
c++;unsigned x=0,y=0;
// printf("merge %d %d = %d\n",u,v,c);
while(true)
{
ll x1,y1,x2,y2;
tie(x1,y1)=nod[u][x];
tie(x2,y2)=nod[v][y];
nod[c].push_back(pl(x1+x2,y1+y2));
if(x+1>=nod[u].size()&&y+1>=nod[v].size()) break;
if(check(u,x,v,y)) x++;else y++;
}
que.push(pi(nod[c].size(),c));
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=m;i++)
{
u[i]=read(),v[i]=read(),a[i]=read(),b[i]=read();
int U=find(u[i]),V=find(v[i]);
if(U!=V) f[U]=V,g.add(u[i],v[i],i),vis[i]=1;
}
dfs(1);
for(int i=1;i<=m;i++) if(!vis[i]) run(i);
for(int i=1;i<=m;i++) if(vis[i]==1)
{
c++;
nod[c].push_back(pi(a[c],b[c]));
que.push(pi(1,c));
}
// printf("c=%d\n",c);
// for(int i=1;i<=c;i++)
// {
// for(auto j:nod[i]) printf("(%lld,%lld) ",j.first,j.second);
// putchar('\n');
// }
while(que.size()>=2)
{
int u=que.top().second;que.pop();
int v=que.top().second;que.pop();
merge(u,v);
}
int P=que.top().second;ll ans=inf;
for(auto i:nod[P]) ans=min(ans,i.first*i.second);
printf("%lld\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9968kb
input:
3 3 1 2 0 1000 2 3 0 1000 3 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 10208kb
input:
5 5 1 2 666 10 2 3 555 1000 3 1 444 345 1 4 222 234 2 5 333 123
output:
3585300
result:
wrong answer 1st numbers differ - expected: '1185480', found: '3585300'