map根据value值排序(用vector序列化
一、7-10 病毒溯源 (25 分)
map按value值排序(用vector序列化)
输入样例:
10
3 6 4 8
0
0
0
2 5 9
0
1 7
1 2
0
2 3 1
输出样例:
4
0 4 9 1
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
const int N=1e4+5;
struct edge{
int to,nex;
}e[N];
int head[N];
int cnt;
void add(int u,int v){
e[++cnt].to=v;
e[cnt].nex=head[u];
head[u]=cnt;
}
int fa[N];
//int d[N];
map<int,int> mp;//<点,距离>
//vector<int> path;
bool cmp(const pair<int,int> &p1, const pair<int,int> &p2) {
return p1.second >p2.second;
}
void dfs(int u,int p){
for(int i=head[u];i;i=e[i].nex){
int to=e[i].to;
if(to==p)continue;
mp[to]=mp[u]+1;
dfs(to,u);
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin>>n;
for(int i=0;i<n;i++){
fa[i]=i;
}
int x,y;
for(int i=0;i<n;i++){
cin>>x;
for(int j=0;j<x;j++){
cin>>y;
add(i,y);
fa[y]=i;
}
}
int s;//源头
for(int i=0;i<n;i++){
if(fa[i]==i){
s=i;
break;
}
}
mp[s]=0;
fa[s]=-1;
dfs(s,-1);
// string res="";
// string tmp="";
vector<pii> v;
for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++)
{
//将map值以pair形式存放在v中
v.push_back(make_pair(it->first,it->second));
}
//按自定义顺序排序
sort(v.begin(),v.end(),cmp);
//输出排序后的vpr结果
//for(vector<pii>::iterator it=v.begin();it!=v.end();it++){
// cout<<it->first<<" "<<it->second<<endl;
//}
vector<int> res;
vector<int> tmp;
vector<pii>::iterator it=v.begin();
x=it->first;
while(x!=-1){
res.push_back(x);
// cout<<"nihao"<<x<<endl;
x=fa[x];
}
reverse(res.begin(),res.end());//这条结果
int len=it->second;//最长路径
it++;
for(;it!=v.end();it++){
tmp.clear();
if(it->second==len){
x=it->first;
while(x!=-1){
tmp.push_back(x);
x=fa[x];
}
reverse(tmp.begin(),tmp.end());//又一条结果
if(tmp<res)res=tmp;
}
else break;
}
cout<<len+1<<endl;
for(int i=0;i<=len;i++){
cout<<res[i];
if(i!=len)cout<<" ";
}
return 0;
}
二、7-11 清点代码库 (25 分),map灵活运用
map容器能盛的多了,依旧是map数组序列化
序列化map< vector< int >,int >,要用vector< pair< vector< int >,int> >
pair< vector< int >,int>
输入样例:
7 3
35 28 74
-1 -1 22
28 74 35
-1 -1 22
11 66 0
35 28 74
35 28 74
输出样例:
4
3 35 28 74
2 -1 -1 22
1 11 66 0
1 28 74 35
之前账号预警那题,学到了map要累计相同东西的个数非常容易,
不 用 e r a s e ( s a m e , 1 ) , 再 i n s e r t ( s a m e , 2 ) , 只 需 要 m p [ s a m e ] + + ; 不用erase(same,1) ,再insert(same,2),只需要mp[same]++; 不用erase(same,1),再insert(same,2),只需要mp[same]++;
对于map<vector<int>,int>
类型也是一样的序列化呀
map<int,int>
要根据第二关键字排序,需要用vector<pair<int,int> >
序列化
而对于map<vector<int>, int>
,对应的要用vector< pair<vector<int>,int> >
序列化
map和pair都是很强哒
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pvi pair<vector<int>,int>
const int N=1e4+5;
bool cmp(const pvi &p1, const pvi &p2) {
if(p1.second==p2.second)return p1.first<p2.first;//字典序从小到大
return p1.second>p2.second;//个数从大到小排
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n,m;
cin>>n>>m;
//vector<vecotr<int> > v(N,vector<int>(105,0)) ;
//每个代码序列要存,相同的累计要起来,vector就不够了,要map
//排序,先按累计的个数,排序是第二关键字是序列字典序
map<vector<int> ,int> mp;
int x;
for(int i=1;i<=n;i++){
vector<int> v;
for(int j=1;j<=m;j++){//对应的m个输出
cin>>x;
v.push_back(x);
}
mp[v]++;
}
//对于map<vector<int>,int>类型也是一样的序列化呀
vector<pvi> v;
for(map<vector<int> ,int>::iterator it = mp.begin(); it != mp.end(); it++)
{
//将map值以pair形式存放在v中
v.push_back(make_pair(it->first,it->second));
}
sort(v.begin(),v.end(),cmp);
//输出排序后的vpr结果
cout<<v.size() <<endl;
for(vector<pvi>::iterator it=v.begin();it!=v.end();it++){
cout<<it->second;
vector<int> a=it->first;
int s=a.size();
for(int i=0;i<s;i++)cout<<" "<<a[i];
cout<<endl;
}
return 0;
}