set_intersection() 的用法 codeforces 245G

本文提供了一个使用C++实现的图论算法示例,包括节点和边的定义、图的建立及处理过程,通过具体代码展示了如何寻找具有最多共同邻居的节点。
#include<bits/stdc++.h>
#define eps 1e-9
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define MAXN 1005
#define MAXM 40005
#define INF 0x3fffffff
using namespace std;
typedef long long LL;
int i,j,k,n,m,x,y,T,ans,big,cas,num,len,an,bn,ans_num,cur;
bool flag;
map <string,int> mp;
vector <int> st[10002];
int vis[10002];
int tmp[10002];
string a,b,rev[10002];
int main()
{
    scanf("%d",&n);
    
    for (i=1;i<=n;i++) st[i].clear();
    mp.clear();
    memset(tmp,0,sizeof(tmp));
    num=0;
    an=bn=0;
    ans_num=0;
    cur=0;
    ans=0;
    
    for (i=1;i<=n;i++)
    {
        cin>>a>>b;
        if (!mp.count(a))
        {
            mp[a]=++num;
            rev[num]=a;
            an=num;
        }else an=mp[a];
        
        if (!mp.count(b))
        {
            mp[b]=++num;
            rev[num]=b;
            bn=num;
        }else bn=mp[b];
        
        st[an].push_back(bn);
        st[bn].push_back(an);
    }
    
    for (i=1;i<=num;i++)
    {
        sort(st[i].begin(),st[i].end()); 
    } 
    
    
    printf("%d\n",num);
    for (i=1;i<=num;i++)
    {
        int pre=0;
        
        cur=0;ans=0;ans_num=0;
        
        memset(vis,0,sizeof(vis));
        for (vector <int> ::iterator it=st[i].begin();it!=st[i].end();it++)
        {
            vis[*it]=1;
        }
        vis[i]=1;
        
        
        for (j=1;j<=num;j++)
        {
            if (vis[j]) continue;
            
            cur=set_intersection(st[i].begin(),st[i].end(),st[j].begin(),st[j].end(),tmp)-tmp;
            if (cur>ans_num)
            {
                ans_num=cur;
                ans=1;
            }else
            if (cur==ans_num)
            {
                ans++;
            }
                
        }
        
        cout<<rev[i]<<" "<<ans<<endl;
    }
     
    return 0;
}

Subtask #1 5ms/2.75MB WA #1 Wrong Answer. 5ms/2.74MB WA #2 Wrong Answer. Subtask #2 139ms/10.61MB AC #3 Accepted, 得分 10. Subtask #3 212ms/12.27MB WA #4 Wrong Answer. Subtask #4 279ms/15.58MB WA #5 Wrong Answer. Subtask #5 229ms/12.21MB WA #6 Wrong Answer. Subtask #6 7ms/2.91MB WA #7 Wrong Answer. 7ms/2.74MB WA #8 Wrong Answer. 7ms/2.75MB WA #9 Wrong Answer. 7ms/2.78MB WA #10 Wrong Answer. 7ms/2.74MB WA #11 Wrong Answer. 7ms/2.75MB WA #12 Wrong Answer. 7ms/2.75MB WA #13 Wrong Answer. 12ms/2.77MB WA #14 Wrong Answer. 16ms/2.92MB WA #15 Wrong Answer. 8ms/2.85MB WA #16 Wrong Answer. Subtask #7 12ms/3.00MB WA #17 Wrong Answer. 12ms/3.10MB WA #18 Wrong Answer. 12ms/3.00MB WA #19 Wrong Answer. 12ms/3.02MB WA #20 Wrong Answer. 12ms/2.98MB WA #21 Wrong Answer. 14ms/3.00MB WA #22 Wrong Answer. 22ms/3.01MB WA #23 Wrong Answer. 56ms/3.10MB WA #24 Wrong Answer. 54ms/3.11MB WA #25 Wrong Answer. 20ms/3.14MB WA #26 Wrong Answer. Subtask #8 168ms/11.67MB WA #27 Wrong Answer. 588ms/16.13MB WA #28 Wrong Answer. 245ms/8.08MB WA #29 Wrong Answer. 590ms/16.70MB WA #30 Wrong Answer.#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; int n, m; ll k; vector<int> adj[100001]; void bfs(int s, vi& dist) { dist.assign(n + 1, -1); queue<int> q; dist[s] = 0; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (dist[v] == -1) { dist[v] = dist[u] + 1; q.push(v); } } } } set<ll> get_all_sp_edges(int s, int t, const vi& dist) { set<ll> edges; vector<bool> visited(n + 1, false); queue<int> q; q.push(t); visited[t] = true; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (dist[v] == dist[u] - 1) { // 关键:允许所有前驱 int a = min(u, v), b = max(u, v); ll key = 1LL * a * 1000000 + b; edges.insert(key); if (!visited[v]) { visited[v] = true; q.push(v); } } } } return edges; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { cin >> n >> m >> k; for (int i = 1; i <= n; i++) adj[i].clear(); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } int sx1, sy1, sx2, sy2; cin >> sx1 >> sy1 >> sx2 >> sy2; vi da, db, dc, dd; bfs(sx1, da); bfs(sy1, db); bfs(sx2, dc); bfs(sy2, dd); if (da[sy1] == -1 || dc[sy2] == -1) { cout << "-1\n"; continue; } int lenA = da[sy1]; int lenB = dc[sy2]; auto ea = get_all_sp_edges(sx1, sy1, da); auto eb = get_all_sp_edges(sx2, sy2, dc); set<ll> common; for (ll e : ea) if (eb.count(e)) common.insert(e); ll c = common.size(); double total = lenA + lenB; if (c > 0 && k > 0) { total = total - 2.0 * c + (2.0 * c * c) / (c + k); } cout << fixed << setprecision(12) << max(total, 1e-9) << '\n'; } return 0; }
10-27
Yqzhu is studying graph theory. He has two forests, called forest 𝐴 and forest 𝐵 , each consisting of 𝑛 nodes numbered from 1 to 𝑛 . He wants to insert the same set of edges into both forests so that the following conditions hold: After inserting an edge, both 𝐴 and 𝐵 are still forests. If an edge ( 𝑢 , 𝑣 ) is inserted into forest 𝐴 , then ( 𝑢 , 𝑣 ) must also be inserted into forest 𝐵 . Among all possible valid edges ( 𝑢 , 𝑣 ) : We first iterate over vertices 𝑢 in increasing order. For each vertex 𝑢 , repeatedly find the smallest vertex 𝑣 > 𝑢 such that inserting edge ( 𝑢 , 𝑣 ) satisfies the above two conditions. Once such a vertex 𝑣 is found, insert the edge ( 𝑢 , 𝑣 ) into both forests and continue searching for the next valid 𝑣 . Yqzhu wants to know the maximum number of edges he can insert and which edges to insert, following the above conditions. Note that a forest is a collection of trees, or equivalently, an undirected graph without cycles. Input Specification The first line contains three positive integers 𝑛 , 𝑚 1 , 𝑚 2 (where 1 ≤ 𝑛 ≤ 100000 and 0 ≤ 𝑚 1 , 𝑚 2 < 𝑛 ), representing the number of nodes, and the number of initial edges in forest 𝐴 and forest 𝐵 . The following 𝑚 1 lines each contains two integers 𝑢 , 𝑣 (where 1 ≤ 𝑢 , 𝑣 ≤ 𝑛 and 𝑢 ≠ 𝑣 ), representing an edge in forest 𝐴 . The following 𝑚 2 lines each contains two integers 𝑢 , 𝑣 (where 1 ≤ 𝑢 , 𝑣 ≤ 𝑛 and 𝑢 ≠ 𝑣 ), representing an edge in forest 𝐵 . Output Specification First, output an integer 𝑘 , representing the maximum number of edges Yqzhu can insert. Then output 𝑘 lines, each line containing two integers 𝑢 and 𝑣 , representing an edge that can be inserted following the conditions. Note that edges should be sorted in alphabetical order. Sample Input Copy 4 1 1 1 2 3 4 Sample Output Copy 2 1 3 2 4 Hint For 50 % of the test cases, 𝑛 ≤ 1000 . For 100 % of the test cases, 𝑛 ≤ 100000 .不能超时
最新发布
11-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值