| 序号 | 分数 |
|---|---|
| A | 48 |
| B | 100 |
| C |
总结
T1
48分思路
代码
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr ll N = 1e7+5,M = 5*N,K=5000;
struct min_tree{
ll u,v,w;
}q[N];
ll p[N];
void init(ll start,ll end){
for(ll i = start;i<=end;i++){
p[i]=i;
}
}
ll find(ll x){
if(p[x]==x) return x;
return p[x]=find(p[x]);
}
bool ifin(ll x,ll y){
return find(x)==find(y);
}
void merge(ll x,ll y){
ll t1=find(x),t2=find(y);
if(t1!=t2) p[t2]=t1;
}
bool min_tree_cmp(min_tree x,min_tree y){
return x.w<y.w;
}
ll n,m,k,idx,cnt,ans;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m >> k;//输入
for(ll i = 1;i<=m;i++){
idx++;//模拟
cin>>q[idx].u >> q[idx].v >> q[idx].w;
}
for(ll i = 1;i<=k;i++){
ll c;cin >> c;//点
for(ll j = 1;j<=n;j++){
ll x;
cin >> x;
q[++idx]={n+i,j,x};//边
}
}
init(1,n+k);//初始化
sort(q+1,q+1+idx,min_tree_cmp);//套板子
for(ll i = 1;i<=idx;i++){
ll u=q[i].u,v=q[i].v,w=q[i].w;
if(!ifin(u,v)){
merge(u,v);
cnt++;
ans+=w;
if(cnt==n+k-1){
cout << ans;
return 0;
}
}
}
return 0;
}T2
题目思路
正确代码
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N = 1e5+5,M = 5*N,K = 5000;
ll n,a[N];
vector<ll> v[N];
queue<ll> q;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1;i<=n;i++){
while(1){
int x;cin >> x;//输入并建图
if(x==0) break;
a[x]++;
v[i].push_back(x);
}
}
for(int i = 1;i<=n;i++) {//开始套板子
if(a[i]==0) q.push(i);//已经没有上级
}
while(q.size()){
int x = q.front();
cout << x << ' ';//输出
q.pop();//出队
for(int i = 0;i<v[x].size();i++){
int y = v[x][i];//代称
a[y]--;//少了一个上级
if(a[y]==0) q.push(y);//入队
}
}
return 0;
}
T3
题目思路
正确代码
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N = 1e5+5,M = 5*N,K = 5000;
int n,a[N],rd[N],ans;
vector<int> g[N];
int dp[N];
int dfs(int x){
if(dp[x]!=0) return dp[x];//出界
dp[x]+=a[x];
int maxn=0;
for(int i = 0;i<g[x].size();i++){//拓补
int y = g[x][i];
maxn=max(maxn,dfs(y));//递归
}
dp[x]+=maxn;//增加
return dp[x];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;//输入
for(int i = 1,x,y;i<=n;i++){
cin >> x >> a[x];
while(1){//建图
cin >> y;
if(y==0) break;
g[x].push_back(y);
rd[y]++;
}
}
for(int i = 1;i<=n;i++){
if(rd[i]==0){
dp[i]=dfs(i);//调用dfs
}
}
int ans = 0;//ans
for(int i =1;i<=n;i++){
ans = max(ans,dp[i]);//比大小
}
cout << ans;
return 0;
}