506D Mr. Kitayuta's Colorful Graph

本文介绍了一个有趣的问题:在一个带有多种颜色边的图中,如何快速找出两点间可通过特定颜色连接的路径数量。通过使用并查集算法,文章提供了一种高效解决方案,并详细解释了其背后的原理及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Mr. Kitayuta's Colorful Graph
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mr. Kitayuta has just bought an undirected graph with n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.

Mr. Kitayuta wants you to process the following q queries.

In the i-th query, he gives you two integers - ui and vi.

Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.

Input

The first line of the input contains space-separated two integers - n and m(2 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting the number of the vertices and the number of the edges, respectively.

The next m lines contain space-separated three integers - aibi(1 ≤ ai < bi ≤ n) and ci(1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj).

The next line contains a integer- q(1 ≤ q ≤ 105), denoting the number of the queries.

Then follows q lines, containing space-separated two integers - ui and vi(1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.

Output

For each query, print the answer in a separate line.

Sample test(s)
input
4 5

1 2 1

1 2 2

2 3 1

2 3 3

2 4 3

3

1 2

3 4

1 4
output
2

1

0
input
5 7

1 5 1

2 5 1

3 5 1

4 5 1

1 2 2

2 3 2

3 4 2

5

1 5

5 1

2 5

1 5

1 4
output
1

1

1

1

2
Note

Let's consider the first sample.

The figure above shows the first sample.
  • Vertex 1 and vertex 2 are connected by color 1 and 2.
  • Vertex 3 and vertex 4 are connected by color 3.
  • Vertex 1 and vertex 4 are not connected by any single color.

题意:有n个点和m条路,每条路都有固定的颜色,给你q次询问,问点u->v有几种走法,每次只能沿一种颜色走。

思路:根据数据的范围,用并查集,可将相同颜色道路所连得点合并,我自己写的代码比较搓,耗时也高,就贴一位大神的代码,其中的M我不知道为啥要分情况讨论,后来才知道妙处

代码://436 ms  8500 KB

#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#define N 100100
#define M 300
using namespace std;
int f[N],ans[N];
pair<int,int> q[N];
vector<pair<int,int> > col[N],total;
map<pair<int,int>,int> light;
int find(int x)
{
    if(f[x]==x) return x;
    return f[x]=find(f[x]);
}
void uni(int x,int y)
{
    if(find(x)==find(y)) return;
    f[find(x)]=find(y);
}
int main()
{
    int n,m,a,b,c,i,j,k,l;
    scanf("%d%d",&n,&m);
    for(i=0; i<m; i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        col[c].push_back(make_pair(a,b));//col[c][i]中存着c颜色道路的a,b点信息
    }
    scanf("%d",&k);
    for(i=0; i<k; i++)
    {
        scanf("%d%d",&q[i].first,&q[i].second);
        if(q[i].first>q[i].second) swap(q[i].first,q[i].second);
        light[q[i]]=0;
    }
    for(i=1; i<=m; i++)
    {
        if(col[i].size()<M)//这里的分情况讨论能大大缩短一些样例的时间
        {
            vector<int> vertex;
            for(j=0; j<col[i].size(); j++)
            {
                vertex.push_back(col[i][j].first);
                vertex.push_back(col[i][j].second);
            }
            sort(vertex.begin(),vertex.end());//都先将同一颜色的点放入集合中,排序并去重用做查找,再用并查集合并道路两定点,最后计算
            vertex.resize(unique(vertex.begin(),vertex.end())-vertex.begin());
            for(j=0; j<vertex.size(); j++)
            {
                f[vertex[j]]=vertex[j];
            }
            for(j=0; j<col[i].size(); j++)
            {
                uni(col[i][j].first,col[i][j].second);
            }
            for(j=0; j<vertex.size(); j++)//这里的复杂度有O(n^n)所以点数不能太大
            {
                for(l=j+1; l<vertex.size(); l++)
                {
                    if(find(vertex[j])!=find(vertex[l])) continue;
                    if(light.count(make_pair(vertex[j],vertex[l])))
                    {
                        light[make_pair(vertex[j],vertex[l])]++;
                    }
                }
            }
        }
        else
        {
            for(j=1; j<=n; j++) f[j]=j;
            for(j=0; j<col[i].size(); j++)

            {
                uni(col[i][j].first,col[i][j].second);
            }
            for(j=0; j<k; j++)//这里复杂度就是max(O(n),O(k) ),而最大10^5,当集合顶点多时候可在k个q询问中寻找
            {
                if(find(q[j].first)==find(q[j].second)) ans[j]++;
            }
        }
    }
    for(i=0; i<k; i++)
    {
        printf("%d\n",light[q[i]]+ans[i]);
    }
    return 0;
}

<think>好的,我现在需要帮助用户解决Content Security Policy(CSP)阻止加载图片的问题。用户提到错误信息是关于CSP阻止加载图片,特别是提到`default-src 'self'`,而`img-src`没有显式设置,导致回退到`default-src`。用户想要解决方案。首先,我要回忆CSP的基本知识,然后结合用户提供的引用信息来分析问题。 首先,根据引用[1]和[2],当CSP中没有显式设置`img-src`时,浏览器会使用`default-src`作为回退。所以如果图片的来源不在`default-src`允许的范围内,就会被阻止。用户遇到的错误可能是因为图片的来源是`blob:`或者`http:`,而`default-src`只允许`self`(即同源)、`data:`、`gap:`等,但可能不包括`blob:`或外部HTTP来源。 接下来,用户提供的引用[4]提到,如果其他资源没有限制,可能需要在CSP中配置。但显然在用户的情况中,`img-src`没有被设置,所以需要显式指定。解决方案应该是明确设置`img-src`指令,包含允许的图片来源,比如`self`、`blob:`,或者特定的域名。例如,如果图片来自blob,就需要添加`img-src 'self' blob:`。 然后,引用[5]指出内联事件处理程序的问题,但用户的问题是图片,所以可能不相关。不过需要注意如果页面中有内联图片的data URI,可能需要`data:`在`img-src`中允许。 用户的问题可能涉及使用`blob:` URL来加载图片,比如通过File API生成的图片。这时候,CSP需要允许`blob:`作为图片来源。因此,正确的做法是在CSP头部中添加`img-src 'self' blob:`,或者根据具体需求添加其他来源。 总结步骤应该是:检查当前CSP配置,确认是否缺少`img-src`,然后添加该指令,包含必要的来源,如`self`、`blob:`、`data:`或特定域名。同时,确保其他相关指令如`default-src`不会覆盖或限制过严。 另外,用户提到错误信息中的`default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'`,这表示`default-src`允许的来源包括同源、data协议、gap协议,以及https://ssl.gstatic.com,但可能不包括`blob:`。因此,显式设置`img-src 'self' blob:`应该能解决问题。 最后,用户可能需要测试不同的配置,使用浏览器开发者工具查看CSP报错信息,逐步调整策略,确保所有需要的图片来源都被允许。</think>根据CSP策略错误提示,您需要调整`img-src`指令配置。以下是具体解决方案: ### 分步解决方案 1. **显式声明`img-src`指令** 在HTTP响应头中,添加`img-src`并明确允许图片来源。例如允许同源、blob协议和特定域名: ```http Content-Security-Policy: img-src 'self' blob: https://example.com; default-src 'self' ``` - `'self'`:允许当前域名的图片来源 - `blob:`:允许通过`Blob URL`加载图片(如File API生成的图片) - `https://example.com`:允许指定域名的图片资源 2. **验证`default-src`的兼容性** 如果保留了`default-src`,需确保其未覆盖`img-src`。根据CSP规则,**显式设置的指令会覆盖`default-src`**[^2]。 3. **处理特殊协议和域名** - 若使用`data: URI`加载图片,需添加`data:`到`img-src` - 若加载HTTP协议图片,需明确添加`http:`或升级为HTTPS(推荐) ### 错误示例分析 假设原始CSP头为: ```http Content-Security-Policy: default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval' ``` 此时浏览器会拒绝加载`blob:http://xxx.png`,因为: - `img-src`未显式设置,回退到`default-src`[^1] - `default-src`未包含`blob:`协议,导致拦截 ### 配置建议 ```http Content-Security-Policy: img-src 'self' blob: data: https://*.cdn.com; script-src 'self' 'unsafe-eval'; object-src 'none'; frame-src https:; default-src 'self' ``` 此配置实现了: - 图片允许当前域名、blob协议、data URI和CDN域名[^4] - 禁止加载任何`<object>`资源[^3] - 框架必须使用HTTPS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值