P2881 [Usaco07mar] ranked cattle ranking the cows

Source: Internet
Author: User
Tags bitset

Bitset Optimizing transitive closure template problem

This relationship is modeled directly on graph theory, which is actually a transitive closure.

Transitive closures There is a simple way to do this is to Floyd.

And the scope of the problem is \ (n \leq 1000\),\ (n^3\) The violence will obviously t.

And the use of bitset, heard can be optimized to the original method of \ (\frac{1}{32}\) even better!

Directly to the code is the fact that you don't understand the principle

#include<cstdio>#include<bitset>const int maxn = 1005;std::bitset<maxn> b[maxn];int n, m;int main(){    scanf("%d%d", &n, &m);    for(int i = 1; i <= n; i++) b[i][i] = true;    while(m--)    {        int u, v; scanf("%d%d", &u, &v);        b[u][v] = true;    }    for(int k = 1; k <= n; k++)    {        for(int i = 1; i <= n; i++)        {            if(b[i][k])            {                b[i] |= b[k];            }        }    }    int ans = 0;    for(int i = 1; i <= n; i++) ans += b[i].count();    ans -= n;    ans = n * (n - 1) / 2 - ans;    printf("%d\n", ans);    return 0;}

P2881 [Usaco07mar] ranked cattle ranking the cows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.