QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#830732#239. Maximum Weighted Matchingpeimuda100 ✓1155ms35072kbC++112.3kb2024-12-24 22:13:042024-12-24 22:13:06

Judging History

你现在查看的是最新测评结果

  • [2024-12-24 22:13:06]
  • 评测
  • 测评结果:100
  • 用时:1155ms
  • 内存:35072kb
  • [2024-12-24 22:13:04]
  • 提交

answer

#include<set>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#define pr pair
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pll pr<ll,ll>
#define pii pr<int,int>
#define piii pr<int,pii>
using namespace std;
const ll m=1e9+7;
struct val
{
	ll a;
	ll v;
	val operator+(val x)
	{
		val rt=a;
		if(a>x.a) rt.v=v;
		else if(a<x.a) rt=x;
		else rt.v=(v+x.v)%m;
		return rt;
	}
	val operator*(val x)
	{
		val rt=a+x.a;
		rt.v=v*x.v%m;
		return rt;
	}
	val(ll x)
	{
		a=x;
		v=1;
	}
	val(){}
	void op()
	{
		cout<<a<<' '<<v<<"  ";
	}
};
struct tag
{
	val a,b,c,d;
	tag operator+(tag x)
	{
		tag rt;
		rt.a=a*x.a;
		rt.b=b*x.a+a*x.b;
		rt.c=c*x.a+a*x.c;
		rt.d=d*x.a+b*x.c+c*x.b+a*x.d;
		return rt;
	}
	tag operator*(tag x)
	{
		tag rt;
		rt.a=a*x.b+c*x.a+a*x.a;
		rt.b=b*x.b+d*x.a+b*x.a;
		rt.c=a*x.d+c*x.c+a*x.c;
		rt.d=b*x.d+d*x.c+b*x.c;
		return rt;
	}
	tag rev()
	{
		tag rt;
		rt.a=a;
		rt.b=c;
		rt.c=b;
		rt.d=d;
		return rt;
	}
	tag(ll x)
	{
		a=0;
		b=-1e18;
		c=-1e18;
		d=x;
	}
	tag(){}
	void op()
	{
		a.op();
		b.op();
		c.op();
		d.op();
		cout<<endl;
	}
};
map<int,tag> p[100005];
set<int> to;
void upd(int x)
{
//	cout<<"Up "<<x<<' '<<p[x].size()<<endl;
	if(p[x].size()==2) to.insert(x);
	else to.erase(x);
}
void ade(int u,int v,tag w)
{
	if(p[u].find(v)==p[u].end())
	{
		p[u][v]=w;
		p[v][u]=w.rev();
	}
	else
	{
		tag&a=p[u][v],&b=p[v][u];
		a=a+w;
		b=b+w.rev();
	}
	upd(u);
	upd(v);
}
void sl()
{
	int n,m;
	cin>>n>>m;
	int u,v;
	ll w;
	tag ls;
	for(int i=0;i<n;i++) p[i].clear();
	to.clear();
	while(m--)
	{
		cin>>u>>v>>w;
		u--;
		v--;
		ls=w;
		ade(u,v,ls);
	}
	while(to.size())
	{
		int f=*to.begin();
		to.erase(f);
		vector<int> g;
		vector<tag> h;
		for(auto i:p[f])
		{
			g.push_back(i.f);
			h.push_back(i.s);
		}
		p[g[0]].erase(f);
		p[g[1]].erase(f);
		p[f].clear();
//		h[0].rev().op();
//		h[1].op();
//		(h[0].rev()*h[1]).op();
//		cout<<"Merg "<<f<<' '<<g[0]<<' '<<g[1]<<endl;
		ade(g[0],g[1],h[0].rev()*h[1]);
	}
	val ans=-1e18;
	tag gt;
	for(int i=0;i<n;i++) if(p[i].size())
	{
		gt=(*p[i].begin()).s;
	}
//	gt.op();
	ans=gt.a+gt.b+gt.c+gt.d;
	cout<<ans.a<<' '<<ans.v<<'\n';
}
int main()
{
	ios_base::sync_with_stdio(0);
	int t;
	cin>>t;
	while(t--) sl();
	return 0;
}/*1
6 10
6 1 1
6 4 1
4 1 1
6 5 1
5 1 1
6 3 1
3 2 1
2 4 1
6 4 1
4 1 1

*/

详细


Pretests


Final Tests

Test #1:

score: 100
Accepted
time: 1155ms
memory: 35072kb

input:

6676
6 10
6 1 1
6 4 1
4 1 1
6 5 1
5 1 1
6 3 1
3 2 1
2 4 1
6 4 1
4 1 1
7 10
4 2 1
4 1 1
1 2 1
1 6 1
6 2 1
1 3 1
3 2 1
1 5 1
5 7 1
7 2 1
7 10
5 3 1
5 7 1
7 2 1
2 3 1
2 1 1
1 4 1
4 3 1
5 6 1
6 7 1
2 3 1
7 10
3 5 1
3 4 1
4 2 1
2 5 1
2 6 1
6 5 1
2 7 1
7 5 1
2 1 1
1 6 1
7 10
5 1 1
5 3 1
3 2 1
2 1 1
2 7 1
...

output:

3 5
3 6
3 14
3 10
3 11
2 13
2 16
3 6
3 3
3 9
2 9
2 14
4 5
3 10
3 13
3 4
3 5
3 14
3 5
2 15
3 10
3 3
3 3
3 14
2 3
3 6
3 3
3 6
3 11
3 17
3 7
3 2
3 6
3 13
3 9
3 11
3 14
3 6
3 2
2 2
2 11
4 4
3 6
3 6
3 5
3 11
2 10
3 5
4 5
2 18
3 13
3 5
3 3
3 12
3 9
2 11
3 2
3 3
3 4
2 7
3 3
3 3
3 2
3 8
3 10
2 15
3 3
3 12
3...

result:

ok 6676 lines